0

Let's take for example the code below:

function createListItemProdus() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('achizitii_produs');            
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('name', some_value);           
    oListItem.update();    
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceededGol), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

From what I've read on the Internet, the load function loads the content of the list from the server to the client.

Then why do I use clientContext.load(oListItem); after oListItem.set_item?

1 Answer 1

1

You don't need to load unless you want to read data.

You can simply set_item then update. Then when you do executeQueryAsync the changes get committed in the back end.

Here is updated code

function createListItemProdus() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('achizitii_produs');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('name', some_value);

    oListItem.update();

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceededGol), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}
3
  • Thank you for you answer, can you help me with one more thing? Can I load more than one item? For example clientContext.load(this.newFile) and clientContext.load(oList), one after another? Apr 27, 2015 at 15:35
  • Yes basically you can stack using load and then executeQueryAsync will do all in one shot. This way communication to server will happen only one time. Apr 27, 2015 at 15:36
  • Thank you very very much for your help, I'll mark your answer as accepted as soon as I can (stackoverflow tells me I have to wait 5 more minutes) Apr 27, 2015 at 15:37

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.