JSOM Batching Code Example

Batching

Batching is one core advantage of JSOM over REST API. Batching minimizes the roundtrips involved by grouping all the queries together.

For example, if you have 10 SharePoint lists to be queried from a page, REST API requires 10 roundtrips which would be taking around 20 seconds. Here we can use JSOM to combine the requests into 1 roundtrip there by reducing the time to 2 seconds. (90% time reduction)

Note: Network Roundtrip occupies the majority time involved in a web page loading.

JSOM Example

In this example we are using 2 lists named Contacts A and Contacts B. Please create these lists from template Contacts & add one item in each.

Contacts A

Contacts B

Now create a new page & add a content editor web part. Insert the following code into it. Change the server URL before saving.

  1. <script src=" http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js" type="text/javascript"></script>  
  2. <script>  
  3. var siteUrl = 'http://server';  
  4. $(document).ready(function ()  
  5. {  
  6.     ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");  
  7. })  
  8.   
  9. function retrieveListItems()  
  10. {  
  11.     var clientContext = new SP.ClientContext(siteUrl);  
  12.     var oList = clientContext.get_web().get_lists().getByTitle('Contacts A');  
  13.     var camlQuery = new SP.CamlQuery();  
  14.     camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');  
  15.     this.collListItem = oList.getItems(camlQuery);  
  16.     clientContext.load(collListItem);  
  17.     var oList2 = clientContext.get_web().get_lists().getByTitle('Contacts B');  
  18.     var camlQuery2 = new SP.CamlQuery();  
  19.     camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');  
  20.     this.collListItem2 = oList2.getItems(camlQuery2);  
  21.     clientContext.load(collListItem2);  
  22.     clientContext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceeded), Function.createDelegate(thisthis.onQueryFailed));  
  23. }  
  24.   
  25. function onQuerySucceeded(sender, args)  
  26. {  
  27.     var listItemInfo = '';  
  28.     var listItemEnumerator = collListItem.getEnumerator();  
  29.     while (listItemEnumerator.moveNext())  
  30.     {  
  31.         var oListItem = listItemEnumerator.get_current();  
  32.         listItemInfo += '\nID: ' + oListItem.get_id() + '\nTitle: ' + oListItem.get_item('Title');  
  33.     }  
  34.     alert(listItemInfo.toString());  
  35.     var listItemInfo2 = '';  
  36.     var listItemEnumerator2 = collListItem2.getEnumerator();  
  37.     while (listItemEnumerator2.moveNext())  
  38.     {  
  39.         var oListItem2 = listItemEnumerator2.get_current();  
  40.         listItemInfo2 += '\nID: ' + oListItem2.get_id() + '\nTitle: ' + oListItem2.get_item('Title');  
  41.     }  
  42.     alert(listItemInfo2.toString());  
  43. }  
  44.   
  45. function onQueryFailed(sender, args)  
  46. {  
  47.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  48. }  
  49. </script>  
Testing the Code

Save and Refresh the page. You can see that the items from both lists are fetched.

run code

message

You can see that the above message boxes shows items from 2 different lists. The code completed in one roundtrip instead of two.

Note: REST is Chattier, but JSOM allows Batching.

References

 

Summary

In this article we have explored a JSOM batching example.