0

I have the following code to retrieve a list item and then to update it.

camlQuery.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{0}</Value></Eq></Where></Query></View>", listItemID);
ListItemCollection collListItem = context.Web.GetList(webrelativeurl + "/lists/" + "FilingSystem").GetItems(camlQuery);
context.Load(collListItem, items => items.Include(
          item => item.Id,
          item => item["Purpose1"],
          item => item.RoleAssignments.Include(roleAssigned => roleAssigned.Member.Title, roleAssigned => roleAssigned.RoleDefinitionBindings)));
context.ExecuteQuery();
foreach (ListItem currentFilingSystemItem in collListItem)
         {
//code goes here....
currentFilingSystemItem.Update();
context.Load(currentFilingSystemItem);
context.ExecuteQuery();

but I am not sure what do these lines do exactly:-

currentFilingSystemItem.Update();
context.Load(currentFilingSystemItem);
context.ExecuteQuery();

now generally speaking when I update sometimes I will save the data to the Database, so why I need to load and to execute the query? and which of this operation will do the actual commit (similar to Save in normal c# code)? Thanks

2

When you call Load, you are only preparing your query. As this article states, in SQL terms think of it of building your SELECT statement.

You then call ExecuteQuery() (or ExecuteQueryAsync()), to submit your prepared query to the remote server and retrieve the specified data.

The difference between ExecuteQuery() and ExecuteQueryAsync() is whether you will wait for the web request to return in-place (in the same execution path) or you will submit a callback function to handle the return when it does come back. It has an impact on how you will structure your code, but not so much on the end result.

So, to be clear:

  • No communication occurs with the remote server until you call ExecuteQuery() or ExecuteQueryAsync() - These operations will do actual commit.
  • You can build up your query by calling Load multiple times, even for properties of different objects, and the client API will construct the appropriate query.
  • You can narrow down the exact properties to fetch so as to be as brief as possible in your web traffic.

Basically these are typical client-server communication patterns, and not at all unique to SharePoint.

  • but in my case i can remove the load and still my object will be saved currentFilingSystemItem.Update(); context.Load(currentFilingSystemItem); context.ExecuteQuery();.. so seems in my case using the load will be optional? and i read that if i use the .load in my case, this mean i will get a newer version of the object, and will not raise a version conflict if i am doing multiple .ExecuteQuery – SharePoint TestDev 2 days ago

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.