Monday, 15 July 2013

Batch Operations using the JavaScript Client Object Model

To improve performance and to reduce the time taken for data to travel over the wire, we can do batch operations using the Client Object Model in SharePoint. This can really help in reducing load time of pages and make the user experience better. Batch operations can be done using the .NET as well as the JavaScript Client Object Model. In this post, we are going to focus on how to perform batch operations using the JavaScript Client Object Model. In each of the following operations, only one call from the client to server will be made. It will contain all the information needed to perform the necessary operation. So without further ado, here is the code:

1) Batch Add Items to List:




2) Batch Update Items from List:



3) Batch Delete Items from List:

Now let us analyze what happens behind the scenes when we make one of these batch operations. Here is the firebug screen grab of the call to batch create 5 items:


Now as you can see, only one call is made from the client to the server. It contains all the XML necessary for creating the 5 items. Here is the relevant XML:


Hope you enjoyed this blog post. Happy SharePointing!

Addendum: I would also like to point out one of the hidden gems of the SharePoint world: if you are using Server Side Code (Full Trust), there is a similar batch method available called SPWeb.ProcessBatchData which can be used to bulk add, edit and delete list items. This is really helpful because it can operate on large number of items at once without making repeated calls to the database.

13 comments:

MÅnµ §îngh said...

Good one ...Liked it... REST API is very powerful. And at least you have not used spservices to do the same.

Anonymous said...

Awsome post. Like the idea of optimizing calls for server from client infrastructure to perform batch operation.

Rambabu Chintapanti said...

NIce and useful article

Shaun Nieves said...

If you have multiple columns, should they all be on the same row within the oListItem.set_Item() line or separate oListItem.set_Item lines?

Vardhaman Deshpande said...

I would go for multiple oListItem.set_Item lines.

Projon said...

Hello,

Can we do this using C# (using Microsoft.SharePoint.dll) ?

Projon said...

Hello

Can we do this using C#(Client object model)?

Raj said...

Thank you Vardhaman. Small doubt.
What if you dont have ID's and If you have to iterate ListItemCollection that you got from a CAML Query to update?

So far to iterate such collection, i have used async's success method. But now, after i get the collection and in iteration, i have to update each item. In this case, do i need to call one more async's success method??

please clarify my doubt.

Vipul Kelkar said...

ItemArray is not required in say, creating list items... clientContext.load(oListItem); should be sufficient since you are loading it in loop..it works without the array. Or may be you have used it for a specific reason which I have not understood ? :)

Rohit Devmore said...

is there a way to track this batch process? e.g. if I send 50 items batch to delete those items, server takes its own time to delete these 50 items and only then we get to know that its done.
I would really like to have counter shown in console.log, when it deletes item, so that end user wont live status.
I tried stupid solution of adding, console.log inside for Loop, but figured out its Async call. Might not be possible at all unless there is inbuilt method to track.
But your insights will be great, oe how you would do it? :).. thank you!

Ulhas Morbale said...

Thanks for the great article, can we do the same for getItems(camlQuery) from different lists? And in OnSuccess() how to handle different listItemCollections?

Something like below,
for (var i = 0; i < listCollection.length; i++) {
var list = list + i;
list = hostContext.get_web().get_lists().getByTitle(listCollection[i]);

listItems = list.getItems(camlQuery);

clientContext.load(listItems);//not sure how this will work
}
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

Kessy Similien said...

Any recommendations on how to retrieve all document libraries from multiple sites within the same collection?

Vardhaman Deshpande said...

@Kessy,

Could you not do it using search?