2

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

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!

2
  • Is the second set of commands inside the success handler for the first? Or do they execute immediately after executeQueryAsync is called? Could you post these all-together?
    – John-M
    Sep 19, 2014 at 2:23
  • In normal case, they would be. For brevity sake I didn't post it in this form. I was running these commands manually in JavaScript console in Chrome, so the ctx.executeQueryAsync definitely completed (since I had a chance to inspect the actual network traffic) before the next set of commands ran.
    – New Dev
    Sep 19, 2014 at 4:00

0

Your Answer

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

Browse other questions tagged or ask your own question.