The SharePoint 2010 – Client Object Model is one of the most interesting new enhancements in SharePoint Foundation 2010 and SharePoint Server 2010. It includes 3 APIs (managed .NET, Silverlight and ECMAScript/JavaScript) allowing you to interact with SharePoint 2010 from a client which hasn’t to be run on a SharePoint server. I had some time to play around with it using resources which were available until the end of 2009.
I’m trying to become a SharePoint 2010 developer using the resources which are available. I will write down my impressions in different articles…
Resources
First of all here are the resources I used:
Videos
The first link includes 6 videos and the second link includes 3 videos with Andrew Connell.
Note: I think the first link provides videos based on SharePoint 2010 alpha or beta 1 and not the public beta… so there might be differences.
MSDN
Blog posts
Steve Peschka has written a really good series:
Using the SharePoint 2010 - Client Object Model
It’s mentioned in Steve Peschka’s series that you need to take care about the amount of data which is sent over the wire. To clarify things I tried it by myself and here are the results:
- The first method I have used receives 22k and includes some bad coding.
- The second method is optimized using a lambda expression and receives about 2k.
- The last method is more optimized and receives only 710 bytes.
Method 1
This method includes some unnecessary code since you don’t need to load the ‘web’, the ‘lists’ and the ‘tasklist’. You only need to use the last ctx.load() statement.
ClientContext ctx = new ClientContext(siteUrl);
Site site = ctx.Site;
Web web = site.RootWeb;
ctx.Load(web);
ListCollection lists = web.Lists;
ctx.Load(lists);
List taskList = lists.GetByTitle("Tasks");
ctx.Load(taskList);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>
Task1</Value></Eq></Where></Query><RowLimit>3</RowLimit></View>";
ListItemCollection listItems = taskList.GetItems(query);
ctx.Load(listItems, items => items.IncludeWithDefaultProperties(item =>
item.DisplayName));
ctx.ExecuteQuery();
Because of unnecessary code the payload (22k) is high compared to the other methods.
Method 2
Removing unnecessary code results in a lower payload (about 2k)
ClientContext ctx = new ClientContext(siteUrl);
List taskList = ctx.Web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>
Task1</Value></Eq></Where></Query><RowLimit>3</RowLimit></View>";
ListItemCollection listItems = taskList.GetItems(query);
ctx.Load(listItems, items => items.IncludeWithDefaultProperties(item =>
item.DisplayName));
ctx.ExecuteQuery();
The lambda expression used in the ctx.load() statement loads all fields of a list items. If you know which columns you need you can use the third method.
Method 3
The difference is the lambda expression where you can select the properties you want to receive before you execute the query.
ClientContext ctx = new ClientContext(siteUrl);
List taskList = ctx.Web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>
Task1</Value></Eq></Where></Query><RowLimit>3</RowLimit></View>";
ListItemCollection listItems = taskList.GetItems(query);
ctx.Load(listItems, items => items.Include(item => item["StartDate"], item =>
item["DueDate"], item => item.DisplayName));
ctx.ExecuteQuery();
The payload is 710 bytes.
Summary
The SharePoint 2010 – Client Object Model is definitely a really cool feature since it allows you to access data from SharePoint with an application that doesn’t need to run on the server. And compared to the SharePoint 2007 beta phase there is enough documentation available to get started.