Monday, March 14, 2011

Client Object Model

Overview :
One of the most promising feature of Sharepoint 2010 is Client object model . Though it still supports object model on server side , client side will play an important role in sharepoint development in coming days .

In 2007 we would either go for server object model to execute code within server where sharepoint is hosted or web services from remote desktops to get rid of a license . However there has been performance issue consuming web services . Hence microsoft came up with a new concept called "Client Object model" where we can consume sharepoint object within remote servers .

Why to use Client Object model ?
Fewer web service calls improves performance.
It can be called from ,NET Code, javascript(ECMAScript) or silverlight.
Reduces network congestion, as there is fewer round trips b/w client and servers.
Gets rid of licensing issues.

How it works :
When we call API's from the client side / silverlight applications, these API's are bundled in to XML by the object model and sent as a request to server for processing . The server after processing sends backs the response[result] as a JSON object , the object model picks up this and converts it to .NET object exposing it to the user. It can be seen that there is a fewer round trips as the entire operation is bundled and executed at once.

Namespaces :
Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime are two prominent namespaces used for consuming object model on the client side using C#.

How to use :
All objects now in client model doesn't begin with SP , i.e : it is Web for SPWeb, List for SPList .The SP concept has been removed in this model. However there are couple of methods that has to be used .
The object whose properties will be accessed in the code has to be first loaded and executed so that they will be ready to access , we will be getting NotInitializedException otherwise.

Load(object whose value properties will be accessed)
ExecuteQuery()

Implmentation ?
using sp = Microsoft.SharePoint.Client;

using (sp.ClientContext objContext = new sp.ClientContext("url"))
{
sp.Web web = objContext.Web;

//Load and execute List to access its properties
objContext.Load(web.Lists);
objContext.ExecuteQuery();
foreach (sp.List oList in web.Lists)
{
//do something to access OList properties

}

Hope you will be now able to build the application on you own .

No comments: