Bulk Update In SharePoint List Using JSOM

In this article I will explain the way for bulk edit in SharePoint list items using JavaScript object model (JSOM)

Steps for Implementation
  • Get Current Context.
  • Get App web URL and Host Web URL from Query string parameter.
  • Calling BulkEdit method in document ready.
  • Get List from app context site.
  • Get list collection by query.
  • Then Update the item by ID.
  • Load the updated item array
  • Finally all item will be updated successfully

In your JavaScript file write the following code,

  1. // Js code Starts here 
  2. ‘use strict’;
  3. //Get Current Context 
  4. var context = SP.ClientContext.get_current();
  5. //Declaring the variables 
  6. varhostWebURL, appWebURL;
  7. // this code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model 
  8. $(document).ready(function()
  9.     {
  10.     // Get App web URL and Host Web URL from Query string parameter 
  11.     hostWebURL = decodeURIComponent(manageQueryStringParameter(‘SPHostUrl’));
  12.     appWebURL = decodeURIComponent(manageQueryStringParameter(‘SPAppWebUrl’));
  13.     //Calling Bulk Edit method in document ready 
  14.     BulkEdit();
  15. });
  16. //Update multiple items
  17. functionBulkEdit()
  18. {
  19.         var title = “Test”;
  20.         vardfd = $.Deferred();
  21.         //Get list from host web 
  22.         varprogramList = appCtx.get_web().get_lists().getByTitle(‘ProgramList’);
  23.         varcamlQuery = newSP.CamlQuery();
  24.         camlQuery.set_viewXml(“<View><Query><Where><Eq><FieldRef Name=’Title’ /><Value Type=’Text’>” + title + “</Value></Eq></Where></Query></View>”);
  25.         //Get items by Query
  26.         varprogColl = programList.getItems(camlQuery);
  27.         //Load item collection
  28.         context.load(progColl);
  29.         context.executeQueryAsync(function()
  30.                 {
  31.                 varcurrentItemArray = [];
  32.                 //Check itemcollection count greater than 0
  33.                 if (progColl.get_count() > 0)
  34.                 {
  35.                     varprogEnum = progColl.getEnumerator();
  36.                     while (progEnum.moveNext())
  37.                     {
  38.                         varcurrentItem = progEnum.get_current();
  39.                         varprogTitle = currentItem.get_item(“Title”);
  40.                         varcurrentId = currentItem.get_item(“ID”);
  41.                         //Get Item by current ID
  42.                         var items = programList.getItemById(currentId);
  43.                         //set isdeleted column to True
  44.                         items.set_item(“IsDeleted”true);
  45.                         //Update the item
  46.                         items.update();
  47.                         //Push item on the array
  48.                         currentItemArray.push(items);
  49.                         //Load the item Array
  50.                         context.load(currentItemArray[currentItemArray.length – 1]);
  51.                     }
  52.                     context.executeQueryAsync(function()
  53.                       {
  54.                         //Item Updated sucess
  55.                         alert(“Items are updated Successfully”);
  56.                         dfd.resolve();
  57.                     }, function(sender, args)
  58.                      {
  59.                         //Item updated fail
  60.                         console.log(“Request Failed to get projectlist Items :” + args.get_message());
  61.                     });
  62.                 } else
  63.                 {
  64.                     dfd.resolve();
  65.                 }
  66.             },
  67.             function(sender, args)
  68.              {
  69.                 $(‘.loader’).hide();
  70.                 console.log(“Request failed in Portfolio List “ + args.get_message());
  71.             });
  72.         returndfd.promise();
  73.     }
  74.     //method for read query string value
  75. functionmanageQueryStringParameter(paramToRetrieve)
  76. {
  77.     varparams = document.URL.split(“?”)[1].split(“&”);
  78.     varstrParams = “”;
  79.     for (var i = 0; i < params.length; i = i + 1)
  80.     {
  81.         varsingleParam = params[i].split(“=”);
  82.         if (singleParam[0] == paramToRetrieve)
  83.         {
  84.             returnsingleParam[1];
  85.         }
  86.     }
  87. }

Summary

In this article we explored how to edit the multiple items on the list using JavaScript object model (JSOM).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s