1

I'm reading a book on app development called "Microsoft SharePoint 2013 App Development" (yeah I know its getting old) and I simply can't wrap my head around the use of ClientContext.Load. An example from the book goes like this (in all examples I have omitted the section where the variable ctx is instantiated with the client context):

ListCreationInformation listCI = new ListCreationInformation();
listCI.Title = "My List";
...
List list = ctx.Web.Lists.Add(listCI);
ctx.ExecuteQuery();

No use of ctx.Load here. But on the next page I get another variant (please ignore the misleading naming of the site variable):

Web site = ctx.Web;
ctx.Load(site);
ctx.ExecuteQuery();

ListCreationInformation listCI = new ListCreationInformation();
listCI.Title = "My Docs";
...
List list = ctx.Web.Lists.Add(listCI);
ctx.ExecuteQuery();

The only difference between the two examples is that a list is added in the first one and a document library is added in the other - no difference in which properties are set on the ListCreationInformation object either, except for the TemplateType.

So why is it necessary to call the Load method on the ClientContext in the second example? A third example calls the Load method on both the web and list object when a call to ctx.Web.Lists.GetByTitle is made - why is it necessary to call it twice in this case?

5

When you create an object representing something in SharePoint (e.g. Web, List, ContentType) it is initially just an empty shell. Most of the properties of the object are in a uninitialized state.

If you want to access the properties of the object you need to request them by calling the Load method of the Context object. The Load method tells the Client Object Model to retrieve the property values and populate them in-place on the next call to ExecuteQuery.

For example, this code will throw an error because the Title property of the Web object has not yet been populated:

Web web = ctx.Web;
Console.WriteLine(web.Title);

If you want to access the Title property you need to call Load and ExecuteQuery.

Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Console.WriteLine(web.Title); 

I should note that only "simple" properties (e.g. string, date, number, boolean) are loaded by default. Collection or complex properties need to be explicitly loaded.

This code would fail because the Lists collection would not have been populated.

Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Console.WriteLine(web.Title);
Console.WriteLine(web.Lists.Count);

To access the Lists property you would need to explicitly load it.

Web web = ctx.Web;
ctx.Load(web);
ctx.Load(web.Lists);
ctx.ExecuteQuery();
Console.WriteLine(web.Title);
Console.WriteLine(web.Lists.Count); 

Getting to your specific question, it does not appear that the properties of the site variable are used during the list creation. If this is the case then the call to Load for the site variable is unnecessary.

I cover this (and much more) in my Client Object Model and REST API course on Pluralsight. If you are not a subscriber you can get a free trial subscription on the website to preview the content.

  • Hi @RobWinsor that makes good sense. But the distinction between simple and complex objects is probably why I don't quite get it. Would it make sense to distinguish between value types and reference types, the latter needing an explicit call to the Load method? I'm a big fan of "shy" code and I don't want to send too much unnecessary data over the wire. – Stig Perez Feb 21 '16 at 8:36
  • And yes I do have a pluralsight account via my MSDN subscription but unfortunately I only get a limited number of courses and yours is off limits :-) – Stig Perez Feb 21 '16 at 8:38
  • Look for the section entitled "Retrieve only selected properties of a website" in this document msdn.microsoft.com/en-us/library/office/fp179912.aspx. It will show you how to explicitly define the values you want to be returned. – Rob Windsor Feb 21 '16 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.