Working with SharePoint Client Object Model

Till SharePoint 2007 we are familiar with SharePoint server object model which is very powerful and allow us to do everything if we have permission to access server physically. For scenarios where our applications can’t be deployed to SharePoint (SP) server but want to use SharePoint as a data source. We go with SharePoint OOB web services (which are limited in functionality) or end up with creating custom services to meet business requirements. In case of custom services, still we need SharePoint server access to deploy our services. Think about scenarios, where it’s impossible to have server access like SharePoint on cloud server (may be one possible scenario). Providing someone access to SharePoint root server on cloud is very risky. Or think about some other department want to use our SharePoint instance. We are forced not to let them deploy there applications on SharePoint server. What could be the possible solution to this? “SharePoint Client Object Model” is the shining star comes with SharePoint 2010.

SharePoint exposes 3 object models:

  1. Managed
  2. Silverlight
  3. ECMAScript (Standardized scripting language like JavaScript or JScript)

Before we go to what each object model do let’s see how client object model works.

When we make a call to SharePoint via client object model,  each client interact with server using service call means client object model makes call to SharePoint service Client.svc (deployed on SharePoint server and act as an interface between Client object model and SharePoint). I hope we all know where SharePoint services are deployed. If don’t know problem you can find it under ISAPI folder of 14 hive. Below is the architectural diagram explains how client access SP server:

Brief about each client object model:

Managed – used to develop custom .NET applications like service, windows applications (WPF), or console applications.

Silverlight – As name signifies it is used by Silverlight applications.

ECMAScript – These are little different than other client object models. These are used inside applications which are hosted inside SharePoint application itself.  In simple terms, cross side scripting is not allowed via ECMAScripts. These scripts should be used on SharePoint pages to access hosting server resources not others. We can use JavaScript or Jscript to achieve same.

Let’s go further deeper. Now we will create applications using each object model to perform CRUD operation on a list.

CRUD operation on a list using Managed Object Model

Step 1: Create a Console application

  1. Open Visual Studio 2010 (Run as Administrator).
  2. Select Windows Console application template (Check you are using .NET Framework 3.5).
  3. Name the project as you like and click ok button.

Step 2: Add SharePoint Client Object Model references

  1. Copy Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll from SharePoint server ISAPI folder (inside 14 hive) to your project folder.
  2. Right click on project and select Add Reference.
  3. On Add Reference window, select Browse tab and navigate to location where you have copied DLL files. Add both files to project.

Step 3: Write necessary code to do CRUD operation

Copy paste code from attached file to Program.cs file and run your project. (Don’t forget to replace siteURL variable with your server name).

Program

This is just a simple example to start with. We can do lot more with Managed client object model.

CRUD operation on a list using Silverlight Object Model

Silverlight Object Model is slightly different in a way we make call to SharePoint from managed object model. Most of the calls made via Silverlight object model are asynchronous; these calls should not update the current running UI thread. If try to do we will get exception.

Another thing before start is we must place the ClientAccessPolicy.xml file in the SharePoint web application virtual directory. Here is content of xml file:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<access-policy>

<cross-domain-access>

<policy>

<allow-from http-request-headers=”*”>

<domain uri=”*” />

</allow-from>

<grant-to>

<resource path=”/” include-subpaths=”true” />

</grant-to>

</policy>

</cross-domain-access>

</access-policy>

Let’s start with our Silverlight application.

Step 1: Create a Silverlight project

  1. Open Visual Studio 2010 (Run as Administrator).
  2. Select Silverlight application template (Check you are using .NET Framework 3.5).
  3. Name the project as you like and click ok button.

Step 2: Add SharePoint Silverlight Client Object Model references

  1. Copy Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll from SharePoint server ISAPI folder (inside 14 hive) to your project folder.
  2. Right click on project and select Add Reference.
  3. On Add Reference window, select Browse tab and navigate to location where you have copied DLL files. Add both files to project.

Step 3: Write necessary code to do CRUD operation

MainPage

Copy paste code from attached file to MainPage.xaml.cs file and run your project. (Don’t forget to replace siteURL variable with your server name).

CRUD operation on a list using JavaScript Object Model

JavaScript object model only works if we use inside current server context I mean from WebParts or Application pages etc. JavaScript object model can’t be used to have cross-site access. This behavior makes it different from above object models. We can also use jQuery in place of JavaScript.

Step 1: Create an empty SharePoint project

  1. Open Visual Studio 2010 (Run as Administrator).
  2. Select SharePoint Empty Solution template (Check you are using .NET Framework 3.5).
  3. Name the project as you like and click ok button.

Step 2: Add Application to project

  1. Right click on project and select Add New Item.
  2. Select Application Page and click ok.

Step 3: Write necessary code to do CRUD operation

JavaScriptOM

  1. Copy paste code from attached file to ContentPlaceHolderID=”PlaceHolderMain” In JavaScriptOM.ascx file.
  2. Press F5 to run project. Modify url to view application page like Http://servername/_Layouts/ApplicationFolderName/ApplicationPageName.aspx
  3. Test each CRUD operation

Thant’s all with the SharePoint Client Object Model. More details can be found on Microsoft MSDN. It’s very well documented there.

Links worth looking at:

Client Object Mode

JS Client Object Model and UI Enhancements

Advertisements

About Rajan
I am Rajan Garg working as a Senior Software Engineer in a company. I am working on the Microsoft technologies.

2 Responses to Working with SharePoint Client Object Model

  1. santu says:

    thanks for the post, well written.

  2. Brian says:

    Very usefull information, I also used this site to solve my problem http://www.sharepointfreehelp.com/2011/02/client-object-model-in-sharepoint-2010/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: