Tuesday, June 11, 2013

Client Object Model vs Server Object Model in SharePoint 2010

Client Object Model 

Client object model is a new feature available in SharePoint 2010. .Client object model provides a  way to do the programming for a SharePoint site using scripting language such as Java Script .


In  Client object model an xml request will be sent  and then server will return JSON which is changed to appropriate object model. 

Mainly 2 assemblies to be referred while working with the Client object model.

Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll

Below is the code sample for a list object from SharePoint site using Client object model :
ClientContext context = new ClientContext("http://sp2010:2012");
 List list = context.Web.Lists.GetByTitle("Title");
context.Load(list);
context.ExecuteQuery();
So, what else a developer can do with Client object model. Client object model provide a way where you can access SharePoint data with scripting language such as Java Script.
You can write simple java script code to perform all those operation. You can use CAML query to access data from SharePoint site.


Using Client object model you can do below tasks:
Get list items, Add list items, Update list items and many more.

Client object model gives result in fast manner, but there is a limitation. The limitation is, we can not access Farm Object using Client object model.
For Client object model silverlight is also an option to program for a SharePoint site.

So if you want to access Farm object then use Server object model.


Server Object Model :



Server object model contains following classes : SPFarm, SPServer, SPSite, SPSiteCollection etc.

Below is the code Sample for Server Object Model:
This code is to get the list items from a SharePoint site:

          using (SPSite oSite = new SPSite(@"http://sp2010:33051"))
            {
                using (SPWeb oWeb = oSite.RootWeb)
                {
                    SPList oList = oWeb.Lists["List1"];  
                    Console.WriteLine("Items in: " + oList);
                    foreach (SPListItem oItem in oList.Items)
                    {
                        string firstname = oItem["Fname"].ToString();
                        Console.WriteLine(firstname);
                    }
                 }
            }

If you want more on Client object model or Server object model then please comment your query. Your comment will be highly appreciated.

3 comments:

Unknown said...

Hi Can you please tell me what is the use client.runtime or what objects we are using from this this DLL.

Anonymous said...

Hi, i need to understand more about Client OM and server OM such as its performance difference and also how we can setup the development environment for both.


Thanks in Advance

Vishesh Singh said...

I am not sure, why you say that client object model provides faster results, although the client object model sends data in the form of XML document to the client.svc service which in term retrieve the results through server object model.
So, I'll better say, using client object model, we have added an extra layer in our data retrieval.
So, definitely server object model is better.