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:
- function BulkItemUpdate() {
- var itemName = "MyItem";
- var ctx = SP.ClientContext.get_current();
- var oWeb = ctx.get_web();
- var oList = oWeb.get_lists().getByTitle('TestList');
- var camlQuery = new SP.CamlQuery();
- camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + itemName + "</Value></Eq></Where></Query></View>");
- var itemColl = oList.getItems(camlQuery);
- ctx.load(oWeb);
- ctx.load(oList);
- ctx.load(itemColl);
- ctx.executeQueryAsync(function() {
- var itemArray = [];
- if (itemColl.get_count() > 0) {
- var itemEnum = itemColl.getEnumerator();
- while (itemEnum.moveNext()) {
- var item = itemEnum.get_current();
- var itemId = item.get_item("ID");
- var items = oList.getItemById(itemId);
- items.set_item("Title", "NewItem");
- items.update();
- itemArray.push(items);
- ctx.load(itemArray[itemArray.length - 1]);
- }
- ctx.executeQueryAsync(function() {
- alert("Items are updated Successfully");
- }, function(sender, args) {
- console.log("Request Failed to get TestList Items :" + args.get_message());
- });
- }
- }, function(sender, args) {
- console.log("Request failed in List " + args.get_message());
- });
- }
Before performing these steps, the list view as shown below.
After performing the bulk update operation, the list item title is now changed to “NewItem”. Refer to the below image.