Using Client Object Model in SharePoint 2010

Using Client Object Model in SharePoint 2010
2 votes, 5.00 avg. rating (96% score)

Introduction

One of the powerfull new feature of Sharepoint 2010 is improved way of accessing data from client side.
When we want to access SharePoint’s data from client side there are two ways

  • In a strongly-typed manner, we can use REST API interface.
  • In a weakly-typed manner, we can use the SharePoint Client Object Model.

Of-course, the old way of using webservices(asmx) is still there, we will jump into new enhanced features.
In this article I will present you how we can use Client Object Model.



Client Object Model can be used in any .NET managed applications even in a Silverlight application, or in any solution that can run ECMAScript code.
Inside concept is, all these platforms will consume a new WCF service named Client.svc, published under the /_vti_bin/ folder of the current site. The service will accept XML requests and respond with JavaScript Object Notation (JSON) responses.

As client object model is new feauture and most of us are not aware of the syntax; I will cover common tasks of a SharePoint list so that we will be familiar with the notation and syntax. In future posts I wil cover some complex scenarios.

Query SharePoint List

The following code explains how we can get List object and query results of it.
Code Example:1

// Create ClientContext object
ClientContext ctxObj = new ClientContext("http://mysitecllection/mysite/");

// Get Site Collection object
Site siteObj = ctxObj.Site;
ctxObj.Load(siteObj);

// Get web object
Web webObj = siteObj.RootWeb;
ctxObj.Load(webObj);

// Build query to the list "EmployeeList"
List empListObj = webObj.Lists.GetByTitle("EmployeeList");
ctxObj.Load(empListObj);

// Execute the query against ClientContext
ctxObj.ExecuteQuery();

// Get the Title
Console.WriteLine(empListObj.Title);

// Build a query for all items in the list
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection allEmploys  = empListObj.GetItems(query);
ctxObj.Load(allEmploys);

// Execute the query against ClientContext
ctxObj.ExecuteQuery();

// Get all the results
foreach (ListItem listItem in allEmploys) {
    Console.WriteLine("Id: {0} - Firstname: {1} - LastName: {2} - Department: {3}",
        listItem["ID"],
        listItem["FirstName"],
        listItem["LastName"],
        listItem["Department"]
        );
}

Code Example:1 Gets the information from Sharepoint List “EmployeeList” with columns as “FirstName”, “LastName” and “Department”. It shows how we can build query with client object and execute the query under client context

Create SharePoint List

In the following snippet we will see how can create a generic SharePoint List “EmployeeList”
Code Example:2

ListItem empItem = empListObj.AddItem(new ListItemCreationInformation());
empItem["FirstName"] = "Adi";
empItem.Update();

ctxObj.ExecuteQuery();

Creating new List item

We will use Code Example:2 code for getting EmployeeList Object, and see how we can create new item to the list
Code Example:3

ListItem empItem = empListObj.AddItem(new ListItemCreationInformation());
empItem["FirstName"] = "Adi";
empItem.Update();

ctxObj.ExecuteQuery();

Update List item

We will use Code Example:2 code for getting EmployeeList Object, and see how we can create update item in the list.

Note: SharePoint does not support SystemUpdate(updates list data without affecting modifying date) method in client object model
Code Example:4

ListItem empItemToUpdate = empListObj.GetItemById(999);

empItemToUpdate["Department"] = "Finance";
empItemToUpdate.Update();

ctxObj.ExecuteQuery();

Delete List item

We will use Code Example:2 code for getting EmployeeList Object, and see how we can create delete item in the list
Code Example:5

ListItem empItemToDelete = empListObj.GetItemById(999);

empItemToDelete.DeleteObject();

ctxObj.ExecuteQuery();

Conclusion

Hope you got some basic understanding of how we can use client object model and quick start your programming to client object model. Happy coding :)

References

ListTemplateType Enumeration
ListItem methods related to SharePoint Client

March 30, 2012 · Adi · No Comments
Tags: , , ,  · Posted in: Client Object Model, Sharepoint 2010

Leave a Reply

What is 14 + 11 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)