4

We are currently building an application and we have to create/update/fetch items in SharePoint lists. We are currently using REST APIs for just about every functionality. But now we need to update multiple items.

I know we can use $.when to batch multiple request and perform (sort of) batch operations with multiple requests to server. But with SP.ClientContext I will have only one request to server. My estimate for number of items to update will be ~50.

As SharePoint does not support batch updates via REST APIs I need to know what will be a good approach to perform multiple update operations - SP.ClientContext OR REST APIs.

1 Answer 1

1

JSOM/CSOM supports batching which is the main highlight of its implementation. You can create/update/delete 50 items in one ExecuteQueryAsync call.

function updateListItems() {
var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('TestList');


for(var i = 1; i<= 5; i++){    
  var oListItem = oList.getItemById(i);  
  oListItem.set_item('Title', 'My Updated Item!' + i);  
   oListItem.update();
   itemArray[i] = oListItem;
   clientContext.load(itemArray[i]);
}
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);


}
function onQuerySucceeded() {
 alert('Items Updated');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

updateListItems();

References

Batch Operations JSOM

JSOM Batching Reference

REST OData v4.0 now Supports batching

You can use $batch query option in REST, to implement batch operations.

REST Batching Reference 1

REST Batching Reference 2

SP 2013 Batch REST

2
  • Thanks for replying. I already know that I can update 50 items in one query in JSOM and have mentioned the same in my question. What I need to know is which one will be a better approach SP.ClientContext OR REST APIs to update multiple items?
    – Naveen
    Commented Aug 7, 2017 at 9:31
  • If you are using $batch in the REST to batch multiple operations, REST is the way to go over CSOM as both implements batching now. Previously REST was chattier than CSOM as it had to do a server roundtrip for each operation. If you read the below link, Andrew has valid reasons why he prefers REST over CSOM and batching was the only demerit for REST when he wrote it few years back. But that has been resolved now : andrewconnell.com/blog/… Commented Aug 7, 2017 at 9:47

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.