SharePoint client object model tutorial

We can use client object model code to retrieve, update, insert and manage data in SharePoint 2016/2013 or SharePoint online.

SharePoint client object model tutorial

SharePoint Online or SharePoint 2016/2013 provides various client object model like below:

  • Managed Client Object Model (csom)
  • JavaScript Client Object Model (jsom)
  • Rest API for SharePoint
  • Silverlight client object model
  • Windows Phone Assemblies

Microsoft provides various dlls or js file to work with SharePoint. To work with managed client object model we have to use two ddls which can be found in ISAPI (E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\) folder.

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

To work with the Silverlight client object model, we have to use below two dlls which are found in LAYOUTS\ClientBin folder.

  • Microsoft.SharePoint.Client.Silverlight.dll
  • Microsoft.SharePoint.Client.Silverlight.Runtime.dll

Similarly to work with JavaScript Object model we have to use SP.js file which is located in the LAYOUTS folder.

While each of the models provides a different programming interface, each interacts with SharePoint through a Windows Communication Foundation (WCF) service named Client.svc, which is located in the ISAPI directory.

sharepoint 2013 client object model tutorial
sharepoint 2013 client object model tutorial

Developers write client-side code using the object model, but the operations are batched and sent as a single Extensible Markup Language (XML) request to the Client.svc service.

When the XML request is received, the Client.svc service makes calls to the server-side object model on behalf of the client. The results of the server-side calls are then sent back to the calling client in the form of a JavaScript Object Notation (JSON) object.

Before starting look at the classes below:

sharepoint online client object model tutorial

Working with Contexts

Server-side object model or client-side object model requires a starting point to work with SharePoint objects. The starting point is known as contexts.

The context object provides an entry point into the associated application programming interface (API) that can be used to gain access to other objects. Once you have access to the objects, you may interact with the scalar properties of the object like Name, Title, Url etc. freely.

The ClientContext class in both the managed and Silverlight object models inherits from the ClientContextRuntime class.

The SP.ClientContext class in the JavaScript client object model inherits from the SP.Client ContextRuntime class.

The ClientContextRuntime class provides two methods for loading objects: Load and LoadQuery.

The Load method specifies an object or collection to retrieve, while the LoadQuery method allows you to return collections of objects using a LINQ query.

Executing the Load or LoadQuery method does not cause the client to communicate with the server. Instead, it adds the load operation to a batch that will be executed on the server. In fact, you may execute multiple load methods (as well as other operations) before calling the server.

Each operation is batched, waiting for your code to initiate communication with the server. To execute the batched operations, your code must call the ExecuteQuery or ExecuteQueryAsync method.

The ExecuteQuery method creates an XML request and passes it to the Client.svc service. The client then waits synchronously while the batch is executed and the JSON results are returned.

The ExecuteQueryAsync method, which is used in the Silverlight client object model, sends the XML request to the server, but it returns immediately. Designated success and failure callback methods receive a notification when the batch operation is complete.

You may like following jsom tutorials: