Implementing MVC pattern in sharepoint
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:
- http://blogs.msdn.com/powlo/archive/2007/02/25/displaying-custom-data-through-sharepoint-lists-using-spgridview-and-spmenufield.aspx (this one is my all time favourite)
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:
- Toolbar Menu Item for creating new item: when user clicks this link he/she must be redirected to the external wizard page.
- Context Menu Item “View”: when user clicks on this tab he/she must be redirected to the external wizard page with some required metadata.
- Context Menu Item “Edit”: when user clicks on this tab he/she must be redirected to the external wizard page with some required metadata.
- 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
- Wrote a server side code which reads the configuration xml file residing in the virtual directory of the sharepoint site
- Locates the xml file and loades it into server cache so to speed up the action.
- Reads the file using xml document class of .NET
- Creates a Hashtable for object data source details (datasource name, assembly details etc), select method name, delete method name etc.
- Creates a Hashtable for insert, update, view, delete url, query string parameters etc.
- Creates a Hashtable for applying filter and sorting on grid columns
- Creates a IList for getting name column name and their metadata which is required for rendering details.
- Creates context menu controls and toolbar menu controls
- Builds the url and query string
- 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.
- Details specified under View heading are jolted into this xml.
- This is set of classes that has all methods required for listing data, insert, update, delete data on the database
- 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.