Home > Design Patterns, General, SharePoint Products and Technology > Implementing MVC pattern in sharepoint

Implementing MVC pattern in sharepoint


Model View Controler

Model View Controler

Hi all,

Today I will dive bit more in depth of Share Point ocean to dig out vital information and knowledge of OOP’s and application architecture in case of share point. Before starting to explore the share point arena I would like to clear few myths and miss-conception about share point.

  • Share Point development is not just about web application development and coding tremendously for building some web application, rather share point is a pre-developed web application(which sets you [developer] free doing tremendous coding for building application) with lot many out-of the box features (this term is many a time miss understood by many developers).
  • Since majority of share point development community comes from traditional web application developer back-ground (even though they are .NET developers), so every one is having his own mind set and thinking regarding web application development which share point has nothing to do with.
  • Many times developers (including me) tend to curse sharepoint because even though it provides some thing called as out-of the box requires good enough coding (may be xml, client side coding, server coding). The best example is BDC 🙂

So because of above reasons (including those which I haven’t specified) working with share point some times becomes a kind of punishment or an unwanted battle. Tell date I haven’t found any best practice for share point development, customization and configuration (now doubt the days are really changing now). This post is in context with the same in which I’m trying to explain how we can implement Design Patterns and Frameworks in case of share point.

Recently I was asked to develop a share point list view (or in a language of .NET developer a fancy gridview which gives share point look-n-feel) now onwards I will refer it as SPGridView control that comes-up with share point. The task was good enough challenging because it was not just about retaining the sharepoint look-n-feel but it should be 100% re-usable control with easy to configure intention and which can be tied to any data source.

So after little bit of googling and using my past experience with SPGridView I started developing a share point web part by which I can achieve the objective.

Below are the links that will guide for developing the web part using SPGridView object:

Majority of the articles stated above bind data to the webpart by creating a object datasource of the sql-server database. This thing proved very helpful for me as my webpart required to be easy for configuration and still rigid. The other requirements were:

  1. Toolbar Menu Item for creating new item: when user clicks this link he/she must be redirected to the external wizard page.
  2. Context Menu Item “View”: when user clicks on this tab he/she must be redirected to the external wizard page with some required metadata.
  3. Context Menu Item “Edit”: when user clicks on this tab he/she must be redirected to the external wizard page with some required metadata.
  4. Context Menu Item “Delete”: when user clicks on this tab he/she must be able to delete the context item/record successfully. Also while performing this action a warning message should be pop-up and based on reply further action should be performed.

So based on the above specified details I decided to go ahead with web part that picks-up the required details from some configuration file (definitely xml) and based on the details renders the data and generates the menu items. After some good amountof brainstorming and paper work I came up with architecture design and to my surprise this design nothing but perfect implementation of MVC / MVP pattern which clearly separates database, data, metadata Model from the Presentation layer whose mere responsiblity is to render the data and this happens through the Controller who acts as perfect mediator in between Model and Presentation/View.

More details about MVC can be found here: http://msdn.microsoft.com/en-us/library/ms998540.aspx and http://www.c-sharpcorner.com/UploadFile/napanchal/MVCDesign12052005035152AM/MVCDesign.aspx

So the only thing’s I did while developing this webpart :

  • View / Presenter
  1. Wrote a server side code which  reads the configuration xml file residing in the virtual directory of the sharepoint site
    1. Locates the xml file and loades it into server cache so to speed up the action.
    2. Reads the file using xml document class of .NET
    3. Creates a Hashtable for object data source details (datasource name, assembly details etc), select method name, delete method name etc.
    4. Creates a Hashtable for insert, update, view, delete url, query string parameters etc.
    5. Creates a Hashtable for applying filter and sorting on grid columns
    6. Creates a IList for getting name column name and their metadata which is required for rendering details.
    7. Creates context menu controls and toolbar menu controls
    8. Builds the url and query string

  • Controller
  1. An xml file which has every minute detail about the underlying model (datasource) so that View / Presentation layer on reading it pulls up proper data and configures it so that listing of data and linking of pages for further data manipulation goes smoothly.
  2. Details specified under View heading are jolted into this xml.

  • Model
  1. This is set of classes that has all methods required for listing data, insert, update, delete data on the database
  2. This set of classes is encapsulted into a assembly which on strong naming is deployed into the GAC of the server so that it becomes accessible by other process and applications.
Advertisements
  1. July 1, 2009 at 11:34 am

    Its a really good and useful article… for sharepoint

  2. Manudhru
    July 31, 2009 at 12:24 pm

    Hi,
    The article is really cool one. Great pointer in direction of patterns

  3. Jai
    April 8, 2010 at 9:04 am

    The article is good pointer towards implementation of MVC

  4. Jai
    April 8, 2010 at 9:04 am

    This is awesome.

  5. November 6, 2010 at 5:26 pm

    Are you a Sharepoint developer? If Yes, then why not Sell your webparts and earn some extra money.
    visit Submit Your WebPart

  6. Raghu
    June 9, 2011 at 3:01 am

    very interested to see the source code..can i get the same

    • manishrao18
      June 10, 2011 at 6:54 am

      hhmm… i tried it previously but since the code block is sprinkled into several files and is bit lengthy so could not post it neatly.. alternatively i’ll host on some file share so that complete src is available. 🙂

  7. July 1, 2012 at 2:40 pm

    Very interesting and nice knowledgeable sharing, I was looking to learn this topic, now I got the solution, looking forward to practically explore it.

  8. April 8, 2013 at 7:20 pm

    Good day! I know this is kinda off topic but
    I was wondering if you knew where I could locate a
    captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  1. October 30, 2012 at 2:14 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: