SharePoint 2010: Getting Started With Client Object Model

Along with the Server Object Model, SharePoint 2010 introduces new set of API called Client Object Model

Refer to my blog post SharePoint 2010: Introducing the Client Object Model to get to know more about Client Object Model.

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.

Lets get started on our ‘Hello Word’ Client Object Model console application.

Step 1 – Reference the Client Object Model

You need to reference two dlls which are available at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

1. Microsoft.SharePoint.Client

2. Microsoft.SharePoint.Client.Runtime

image

Add the using namespace for Microsoft.SharePoint.Client

using Microsoft.SharePoint.Client;

Step 2 – Create the Client Context

[ We will use the Main function to write our piece of code ]

You need to first create a context to the SharePoint site:

ClientContext context = 
new ClientContext(http://<your-site>);
Step 3 – Create Your Query

Here is the next block of code, explanations will follow below:

Web site = context.Web;
context.Load(site, s => s.Title);

As you can see, we are querying for what we require and nothing else will be returned. In our code, we pass the site to the context, but say we want to query only the property Title.

Step 4 – Execute Your Query

Below is the code to execute the query:

context.ExecuteQuery();

The ExecuteQuery() method is a synchronous method call.

Step 5 – The Output

Below is the output:

image

Another Query Example

ListCollection lists = site.Lists;
IEnumerable<SP.List> listsCollection =
context.LoadQuery(lists.Include(
l => l.Title,
l => l.Id));

The above code queries all the Lists in the site, but only returns the Title and the Id properties.

In the next post, we will see how we can query asynchronously.


Comments

Oct 31 Bryan

Bryan

Great post.

Will the Client Object Model only work against SharePoint 2010, or will it also be compatible against SharePoint 2007?

Oct 31 chakkaradeep

chakkaradeep

@Bryan - Client OM will work only with SharePoint 2010.

Nov 27 Naresh

Naresh

Hi,

I am doing a ASP.NET web site, in which i am trying to use the SP2010 data.

My web site is running on Integrated Windows Authentication, But i am getting the access denied error while conneting to sharepoint using client object model.

What to do?

Thanks,
NB

www.onlinespielautomat.com

Microsoft Office 2010 and it isn’t going to be released alone. Accompanying it will be Office Web Applications and the SharePoint 2010 collaborative platform as well.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading





Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.