Getting Started With SharePoint CSOM

Getting Started With SharePoint CSOM - Codeculous.
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.
Watch Video Demo
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.
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.
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.
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,
  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).
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

using Microsoft.SharePoint.Client;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace RK_CSOM_Demo  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // ClientContext - Get the context for the SharePoint Site  
            // SharePoint site URL - http://RKTest.com/sites/testsite/  
            ClientContext clientContext = new  
                ClientContext("http://RKTest.com/sites/testsite/");  
  
            // Get the SharePoint web  
            Web web = clientContext.Web;  
              
            // Get the SharePoint list collection for the web  
            ListCollection listColl = web.Lists;  
              
            // Retrieve the list collection properties  
            clientContext.Load(listColl);  
  
            // Execute the query to the server.  
            clientContext.ExecuteQuery();  
              
            // Loop through all the list  
            foreach (List list in listColl)  
            {  
                // Display the list title and ID  
                Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);  
            }  
            Console.ReadLine();  
        }  
    }  
}  

Output
Codeculous - Getting Started With SharePoint CSOM
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



using Microsoft.SharePoint.Client;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace RK_CSOM_Demo  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            ClientContext clientContext = new  
                ClientContext("http://RKTest.com/sites/testsite/");  
  
            List list = clientContext.Web.Lists.GetByTitle("RK_List");  
  
            CamlQuery query = new CamlQuery();  
            query.ViewXml = "";  
            ListItemCollection items = list.GetItems(query);  
  
            clientContext.Load(list);  
            clientContext.Load(items);  
  
            clientContext.ExecuteQuery();  
  
            foreach (ListItem item in items)  
            {  
                Console.WriteLine(item.Id + " - " + item["Name"]);  
            }  
  
            Console.ReadLine();  
        }  
    }  
}  

Output
Codeculous - Getting Started With SharePoint CSOM Insert an Item

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

Source Code



List list = clientContext.Web.Lists.GetByTitle("RK_List");  
ListItemCreationInformation newItem = new ListItemCreationInformation();  
ListItem listItem = list.AddItem(newItem);  
listItem["Title"] = "Learn Beyond Academics";  
listItem["Name"] = "Codeculous";  
listItem["Location"] = "Delaware";  
listItem.Update();  
clientContext.ExecuteQuery();  

Output
After executing the query, please refresh the list page.
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


foreach (ListItem item in items)  
{  
    if (item.Id.ToString() == "1")  
    {  
        item["Location"] = "Delaware";  
        item.Update();  
    }  
}  
clientContext.ExecuteQuery();  

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

Source Code


foreach (ListItem item in items)  
{  
    if (item.Id.ToString() == "1")  
    {  
        item.DeleteObject();  
        break;  
    }  
}  
clientContext.ExecuteQuery(); 

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

Codeculous - Getting Started With SharePoint CSOM

Post a Comment

Thanks for your comments.

Previous Post Next Post