0

I'm using SharePoint Javascript Object Model (JSOM). Can someone explain to me what set_item() does when update() is not called?

Below is some code to illustrate.

Boilerplate code to get the list items:

var ctx = SP.ClientContext.get_current();

var list = ctx.get_web().get_lists().getByTitle('Employees');
var listItems = list.getItems("");

ctx.load(listItems, "Include(Name, Role)");
ctx.executeQueryAsync(onSuccess, onFailure);

first set of commands

var first = listItems.getItemAtIndex(0);
var second = listItems.getItemAtIndex(1);

first.set_item("Role", "foo");
second.set_item("Role", "bar");

second.update(); // only call update on the second item
ctx.executeQueryAsync(onSuccess, onFailure);

The actual network request to SharePoint contains two "SetFieldValue" methods with "foo" and "bar", and only one "Update" method for the second one. SharePoint only updated the second value with "bar".

second set of commands

first.update();
ctx.executeQueryAsync(onSuccess, onFailure);

The second request contains only a single Update method. SharePoint did not update the first value with "foo".

Question

Why is the first item sent even though update was not called?

Clearly, when first.update() is called, the new value is not passed again - but I would at least assume that it would be staged in SharePoint, waiting for the update() - but that doesn't happen.

Thanks!

1 Answer 1

1

Executing update() cues the backend to actively replace/add values to the stored object; set_item() passes the value and field to be updated to the server. Both are working as intended, but no actual change will occur to the stored information until you utilize update() explicitly. This may be in order to better utilize batch processing.

7
  • True, but why would a change log from set_item go over the network if there was no way (that I can see) to utilize it?
    – New Dev
    Oct 7, 2014 at 19:35
  • I would hazard that it's probably purely preparatory. It may also (and mind you I'm hypothesizing at this point) be useful in terms of asynchronous development. Programmatically speaking, it's a very clear syntax to set update execute. This provides a convenient control flow; additionally, you can set multiple items to queue up for the update() method.
    – Kyle
    Oct 7, 2014 at 19:51
  • I tried the latter - it didn't work. Which was the reason it puzzled me.
    – New Dev
    Oct 7, 2014 at 19:55
  • Although, I see what you're saying regarding the unnecessary network traffic. However, the asynchronous nature of the program may require you to set those items into the backend queue, and then execute the update method later through an asynchronous chain.
    – Kyle
    Oct 7, 2014 at 19:55
  • Yeah, I tried that, by calling calling update at a later point, and that had not effect.
    – New Dev
    Oct 7, 2014 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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