SharePoint Client Object Modal (CSOM)

Introduction

Client Side Object Model (CSOM) was first introduced in SharePoint 2010. The Client Side Object Model is mainly used to build client applications and enable us to access SharePoint Sites that are hosted outside without using web services. Prior to the CSOM, developers had only a few choices to build client applications.

However, this has changed now. With the introduction of CSOM, developers now have more choices and will be able to access the core SharePoint functionalities to a wide extent. In SharePoint 2010, the CSOM exposed the core SharePoint functionalities only whereas in SharePoint 2013, the Microsoft SharePoint team has added a few more assemblies. Now we are able to access the Service Applications using the Client Side Object Model.
model
There are three programming models used for the Client Side Object Model; they are:

  • .Net Client Side Object Model.
  • JavaScript Object Model.
  • Silverlight Object Model.

model 

Object OM

Location

Names

Managed

ISAPI folder

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

Silverlight

Layouts\ClientBin

Microsoft.SharePoint.Client. Silverlight.dll

Microsoft.SharePoint.Client.

Silverlight.Runtime.dll

JavaScript

Layouts

SP.js

Assemblies for the above models are located in the SharePoint file system. The .Net Client Side Object Model is located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI. The Silverlight Object Model is located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\ClientBin. ECMAScripts are located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15 \TEMPLATE\LAYOUTS.

How CSOM works

If you use the Client Side API to perform a specific task then the SharePoint .Net client object model bundles up these uses of the API into XML and send them to the server. The server receives this request, and makes appropriate calls into the object model on the server, collects the responses, forms them into JavaScript Object Notation (JSON), and sends that JSON back to the SharePoint .Net client object model.

The client object model parses the JSON and presents the results to the application.
csom
Load() method: the Load() method does not actually load anything, only when the ExecuteQuery() method is called does it provide notification that these are the property values that should be loaded for the object. In the Load() method lambda expressions can be used to specify which property should be loaded instead of loading all the properties.

ExecuteQuery() method: the ExecuteQuery() method sends the request to the server. There is no network traffic until the application calls this method. Until the ExecuteQuery() method is called only the requests are registered by the application.

csom

Create a console application using Visual Studio

In this section you will see how to create a console application using Visual Studio 2012 which will be used to explain all the examples for the .Net Client Side Object Model.

Steps Involved:

  1. Run Visual Studio as Administrator
  2. Create a Console Application,

    Application

  3. In the Solution Explorer, right-click on the “References” folder and then click on “Add Reference”.
  4. Add the following assemblies from hive 15 (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI).

    solution

Perform SharePoint list tasks using CSOM

In this section you will see how to perform list related tasks using the SharePoint 2013 .Net Client Side Object Model.

In this example you will see how to get all the lists from the website using the .Net Client Side Object Model.

Replace Program.cs with the source code below and run the application.

Source Code

  1. using Microsoft.SharePoint.Client;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace RK_CSOM_Demo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             // ClientContext - Get the context for the SharePoint Site  
  15.             // SharePoint site URL - http://RKTest.com/sites/testsite/  
  16.             ClientContext clientContext = new  
  17.                 ClientContext("http://RKTest.com/sites/testsite/");  
  18.   
  19.             // Get the SharePoint web  
  20.             Web web = clientContext.Web;  
  21.               
  22.             // Get the SharePoint list collection for the web  
  23.             ListCollection listColl = web.Lists;  
  24.               
  25.             // Retrieve the list collection properties  
  26.             clientContext.Load(listColl);  
  27.   
  28.             // Execute the query to the server.  
  29.             clientContext.ExecuteQuery();  
  30.               
  31.             // Loop through all the list  
  32.             foreach (List list in listColl)  
  33.             {  
  34.                 // Display the list title and ID  
  35.                 Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);  
  36.             }  
  37.             Console.ReadLine();  
  38.         }  
  39.     }  
  40. }  
Output

Output

CRUD operations examples with Client Object Model

Here I would like to list some examples using the Client Object Model. For starting with the examples, please do the following:

Get List Items

Here we are querying the list items of the Tasks list.

Source Code
  1. using Microsoft.SharePoint.Client;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace RK_CSOM_Demo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             ClientContext clientContext = new  
  15.                 ClientContext("http://RKTest.com/sites/testsite/");  
  16.   
  17.             List list = clientContext.Web.Lists.GetByTitle("RK_List");  
  18.   
  19.             CamlQuery query = new CamlQuery();  
  20.             query.ViewXml = "<View/>";  
  21.             ListItemCollection items = list.GetItems(query);  
  22.   
  23.             clientContext.Load(list);  
  24.             clientContext.Load(items);  
  25.   
  26.             clientContext.ExecuteQuery();  
  27.   
  28.             foreach (ListItem item in items)  
  29.             {  
  30.                 Console.WriteLine(item.Id + " - " + item["Name"]);  
  31.             }  
  32.   
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  
Output

Output

Insert an Item

Here we can try inserting a new item into the Tasks list.

Source Code
  1. List list = clientContext.Web.Lists.GetByTitle("RK_List");  
  2.   
  3. ListItemCreationInformation newItem = new ListItemCreationInformation();  
  4. ListItem listItem = list.AddItem(newItem);  
  5. listItem["Title"] = "Kiran";  
  6. listItem["Name"] = "Kiran";  
  7. listItem["Location"] = "Bangalore";  
  8. listItem.Update();  
  9. clientContext.ExecuteQuery();  
Output

After executing the query, please refresh the list page. You can see the following result.

Output

Update List Item

Here I would like to show the modification code. All the titles are appended with two asterisks whose IDs are even number.

Source Code
  1. foreach (ListItem item in items)  
  2. {  
  3.     if (item.Id.ToString() == "1")  
  4.     {  
  5.         item["Location"] = "Bangalore";  
  6.         item.Update();  
  7.     }  
  8. }  
  9. clientContext.ExecuteQuery();  
Output

After executing the query, please refresh the list page. You can see the following result.

Output

Delete an Item

Now we can try deleting an item from the List. Here is the code to achieve that.

Source Code
  1. foreach (ListItem item in items)  
  2. {  
  3.     if (item.Id.ToString() == "1")  
  4.     {  
  5.         item.DeleteObject();  
  6.         break;  
  7.     }  
  8. }  
  9. clientContext.ExecuteQuery();  
Output

After executing the query, please refresh the list page. You can see the following result.
 
Output

Read more articles on SharePoint,