Tuesday, July 17, 2012

Using LINQ in Client object model

In this post, I would like to demonstrate a simple LINQ queries to access the SharePoint data using client object model.
ClientContext clientContext = new ClientContext("http://Test:555");
var query = from list in clientContext.Web.Lists
where list.Hidden == false
select list;
IEnumarable lists=clientContext.LoadQuery(query);
clientContext.ExecuteQuery();
MessageBox.Show(clientContext.Web.Lists.Count.ToString());
Another sample which uses Load method and lambda expressions

ClientContext clientContext = new ClientContext("http://Test:555");
clientContext.Load(clientContext.Web.Lists, lists => lists
.IncludeWithDefaultProperties(list => list.RootFolder,
list => list.Fields));

The difference between Load and LoadQuery are
Load: Using method syntax of Linq query it will fills out the objects in the context.

LoadQuery: This method returns custom object and it will not filled into the context. 


No comments:

Post a Comment