Sunday, July 6, 2014

SharePoint 2013 - The .Net Client-Side Object Model (CSOM)

In this post, we will see an overview of .net client-side object model (CSOM). And in the next post we will see demo to retrieve all items from a SharePoint list using .Net Client-Side Object Model (CSOM).

Classes in Microsoft.SharePoint.Client
As compared to server object model, the client side object model has a lot of identical classes with a suffix in the namespace and no SP prefix in the class name.
Client Object ModelServer Object Model
ClientContextSPContext
SiteSPSite
WebSPWeb
ListSPList


Locate the assemblies required for implementing .Net CSOM
Set of managed assemblies for CSOM reside in SharePoint15_Root\ISAPI. The following 2 assemblies are the minimum required assemblies for CSOM.
(i) Microsoft.SharePoint.Client.dll
(ii) Microsoft.SharePoint.Client.Runtime.dll


The ClientContext instance
An instance of the ClientContext class in Microsoft.SharePoint.Client namespace should be created to refer to the target Site Collection. ClientContext class is equivalent to SPContext class in object model and stands as proxy to the SharePoint server that you are targeting
ClientContext ctx = new ClientContext("http://SiteCollectionUrl/");

Authenticating the ClientContext instance
By default, the CSOM uses Windows integrated authentication. The ClientContext class, through its ClientRuntimeContext base class, provides an AuthenticationMode property and a FormsAuthenticationLoginInfo property, which are useful to configure a set of forms-based authentication credentials.
NetworkCredential credentials = new NetworkCredential("username""password""domain")
ctx.Credentials = credentials;

Loading the objects using Load<T> method
Whenever we need to access an object, we have to first request to load that object using Load<T> method of the ClientContext instance. The following example shows how to load the site, web and list objects.
Site site = ctx.Site;
ctx.Load(site);
Web web = site.RootWeb; 
ctx.Load(web);
List list = web.Lists.GetByTitle("Products");
ctx.Load(list);

The ExecuteQuery Method
Once all the objects are loaded, the ExecuteQuery method should be invoked to complete the action on the site, web or list. There is also an asynchronous version of this method, called ExecuteQueryAsync, for invoking the service asynchronously.
ctx.ExecuteQuery();

Adding CAML Query
To improve performance and reduce network traffic, the data retrieval engine of the Client Object Model by default does not retrieve all of the properties of the items you load. You can use the CAML query definition to specify the fields to retrieve, setting the ViewFields property.
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection allItems = list.GetItems(query);
ctx.Load(allItems);
ctx.ExecuteQuery();

Hope this post gives you an idea about the .Net Client-Side Object Model (CSOM). In the next post we will see demo to retrieve all items from a SharePoint list using .Net Client-Side Object Model (CSOM).

4 comments:

  1. Interesting piece of information, I had come to know about your web-page from my friend, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that I had been looking for, I'm already your rss reader now and I would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regards, SharePoint Training Institutes in Hyderabad India

    ReplyDelete
  2. Awesome piece of information, I had come to know about your website from my friend, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that I had been looking for, I'm already your rss reader now and I would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again, Regards, SharePoint Online Training in Hyderabad India

    ReplyDelete
  3. Awful piece of information, I had come to know about your blog from my friend, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that I had been looking for, I'm already your rss reader now and I would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regards, SharePoint Developer Training in Hyderabad India

    ReplyDelete
  4. Thanks Everyone!! I am really glad you liked it!!

    ReplyDelete

Note: Only a member of this blog may post a comment.