Bulk Update SharePoint List Items Using JSOM

Introduction 

 
In this blog, we will discuss bulk updating SharePoint list items. We are going to use JSOM code to perform the above action. Bulk updates are very efficient, as any kind of huge changes or updates in the list & list items can be done within a fraction of second. In this blog, we have updated the items’ Title from “MyItem” to “NewItem” i.e. in the list which item Title is “MyItem” it will be changed to “NewItem”.
 
To perform this tutorial, please refer to the below steps.
 
Step 1
 
First, get the Current Context.
 
Step 2
 
Get the list by its “Title” in which you want to update the list Items (In this programmed I used the List 'Testlist').
 
Step 3
 
To Load the item collection, we used CamlQuery.
 
Step 4
 
Declared an ItemArray for the bulk update.
 
Step 5
 
We retrieved all the items by using the enumerator.
 
Step 6
 
We updated the bulk items.
 
Follow the complete code:
  1. function BulkItemUpdate() {  
  2.     var itemName = "MyItem";  
  3.     var ctx = SP.ClientContext.get_current();  
  4.     var oWeb = ctx.get_web();  
  5.     var oList = oWeb.get_lists().getByTitle('TestList');  
  6.     var camlQuery = new SP.CamlQuery();  
  7.     camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + itemName + "</Value></Eq></Where></Query></View>");  
  8.     var itemColl = oList.getItems(camlQuery);  
  9.     ctx.load(oWeb);  
  10.     ctx.load(oList);  
  11.     ctx.load(itemColl);  
  12.     ctx.executeQueryAsync(function() {  
  13.         var itemArray = [];  
  14.         if (itemColl.get_count() > 0) {  
  15.             var itemEnum = itemColl.getEnumerator();  
  16.             while (itemEnum.moveNext()) {  
  17.                 var item = itemEnum.get_current();  
  18.                 var itemId = item.get_item("ID");  
  19.                 var items = oList.getItemById(itemId);  
  20.                 items.set_item("Title""NewItem");  
  21.                 items.update();  
  22.                 itemArray.push(items);  
  23.                 ctx.load(itemArray[itemArray.length - 1]);  
  24.             }  
  25.             ctx.executeQueryAsync(function() {  
  26.                 alert("Items are updated Successfully");  
  27.             }, function(sender, args) {  
  28.                 console.log("Request Failed to get TestList Items :" + args.get_message());  
  29.             });  
  30.         }  
  31.     }, function(sender, args) {  
  32.         console.log("Request failed in List " + args.get_message());  
  33.     });  
  34. }  
Before performing these steps, the list view as shown below.
 
Bulk Update In SharePoint List Items Using JSOM
 
After performing the bulk update operation, the list item title is now changed to “NewItem”. Refer to the below image.