Sharepoint 2010 Object model Tutorial – II

Part 1 Part 2 Part 3

Now, lets talk about the famous Client -side object model of SharePoint 2010.

Firstly the definition :

“The Client Object Model (OM) is a new programming interface for SharePoint 2010 where code runs on a user’s client machine against a local object model and interacts with data on the SharePoint Server. Client OM methods can be called from JavaScript, .NET code or Silverlight code and makes building rich client applications for SharePoint easy.”

To develop rich client side solutions, three set of client-side APIs has been introduced in the Microsoft.SharePoint.Client namespace. The three APIs are targeted for three different types of clients:

1. For .net Managed applications (for example, console applications, window applications, web applications etc, which are not running inside SharePoint Context).

2. For Silverlight applications.

3. For using with JavaScript (called ECMAScript). This API is only available for applications hosted inside SharePoint (for example, web part deployed in SharePoint site can use this JavaScript API for accessing SharePoint from browser using JavaScript).

Before, we go further in explaining these three methods, lets see how this Client object model works ? Here is a good explanation from msdn – When we use SharePoint client API’s to perform a specific task, the SharePoint Foundation 2010 managed client object model bundles up these uses of the API into XML and sends it to the server that runs SharePoint Foundation. Side note -Client OM under the hood uses Client.svc WCF service to communicate with SharePoint foundation. 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 Foundation 2010 managed client object model. The client object model parses the JSON and presents the results to the application as .NET Framework objects (or ECMAScript objects for ECMAScript).

Lets look at some important notes about the three methods we are going to use -

.NET Managed - The API’s are available to create managed .NET Windows(or client) applications using .NET framework 3.5 and higher.
To communicate with .NET Managed Client OM you need to add Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll in your project. These can be found @ C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI location.

Silverlight - The API’s available is used to create Silverlight applications that use SharePoint data and objects. (Silverlight 2 and newer versions). To communicate with the SharePoint server in Silverlight client context we need to add a reference to Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll.
The dlls can be found at “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin”.
See the Post Create and deploy silverlight webpart in sharepoint 2010

ECMAScript Client OM – Is a client object model extension for using JavaScript or JScript. This API is only available for applications hosted inside SharePoint (for example, web part deployed in SharePoint site can use this JavaScript API for accessing SharePoint from browser using JavaScript). We will discuss the ECMA script model in detail in next part of the Tutorial.

Lets start with –
.NET Managed - client object model.
This object model was introduced to create console applications, window applications, web applications etc, which are not running inside SharePoint Context. One of the main advantages of using this object model(infact the whole client object model) is that you can execute or line up all the server tasks in one SharePoint server server call. It returns you Json object that you can bind to a client side object.

Lets jump a quick code snippet now.

Add List Items – The example below uses a CAML query to extract list items from “mycustomList” in SPSite site collection.

ClientContext clientContext = new ClientContext(“http://SPSite”);
List list = clientContext.Web.Lists.GetByTitle(“mycustomList”);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @”<View><Query><Where>
<Eq><FieldRef Name=’Category’/><Value Type=’Text’>Development</Value>
</Eq></Where></Query><RowLimit>100</RowLimit></View>”;

ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Category"]));
clientContext.ExecuteQuery();
foreach (ListItem listItem in listItems)
{
Console.WriteLine(“Title: {0}”, listItem["Title"]);
Console.WriteLine(“Category: {0}”, listItem["Category"]);
Console.WriteLine();
}
Next we will look at the ECMA Script object model

Related Posts