Update SharePoint List Items Using SPServices And Batch

Introduction

This article introduce how to update SharePoint list items using SPservice.

Requirement

If client has provided SharePoint site with restricted access and want to customize the site then we can’t write C# code or any server side code. Using SharePoint Web services and JQuery we can develop custom pages to update items instead Web parts.

Here, I am providing one example to update the SharePoint list items. Please follow the steps.

Step 1:
Here I am taking a list called ‘school’ and it has columns Title, Phone.

List
Step 2: Open SharePoint designer and create a page in SitePages library and apply master page to this page.

Step 3: Download SPService reference file and JQuery reference from source of this article and add to the page.

Step 4: Write the following code in above created page.

  1. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  
  2. <script type="text/javascript" src="https://<siteurl>/SiteAssets/jquery.SPServices-2014.02.js"></script>  
  3. <script type="text/javascript">  
  4. $(document).ready(function ()  
  5. {   
  6.     loaditems();  
  7. });  
  8.   
  9. function loaditems()  
  10. {  
  11.     var tbl = "<table>";  
  12.     $().SPServices(  
  13.     {  
  14.         operation: "GetListItems",  
  15.         async: false,  
  16.         listName: "school",  
  17.         //CAMLViewFields : "<ViewFields><FieldRef Name='Title' /></ViewFields>",  
  18.         completefunc: function (xData, Status)  
  19.         {  
  20.             alert(xData.responseText);  
  21.             alert(xData.status);  
  22.             if (xData.status == 200)  
  23.             {  
  24.                 $(xData.responseXML).SPFilterNode("z:row").each(function ()  
  25.                 {  
  26.                     tbl += "<tr><td>" + $(this).attr("ows_ID") + "</td><td>" + $(this).attr("ows_LinkTitle") + "</td><td>" + $(this).attr("ows_Phone") + "</td></tr>";  
  27.                 });  
  28.             }  
  29.         }  
  30.     });  
  31.     tbl += "</table>";  
  32.     $("#divHtml").html(tbl);  
  33. }  
  34.   
  35. function updateListContent()  
  36. {  
  37.     var title = $('#txtTitle').val();  
  38.     var phone = $('#txtPhone').val();  
  39.     var itmid = $('#txtid').val();  
  40.     $().SPServices(  
  41.     {  
  42.         operation: "UpdateListItems",  
  43.         async: false,  
  44.         batchCmd: "Update",  
  45.         listName: "school",  
  46.         updates: "<Batch OnError='Continue' PreCalc='TRUE' >" + "<Method ID='1' Cmd='Update'>" + "<Field Name='ID' >" + itmid + "</Field>" + "<Field Name='Title' >" + title + "</Field>" + "<Field Name='Phone' >" + phone + "</Field>" + "</Method></Batch>",  
  47.         completefunc: function (xData, Status)  
  48.         {  
  49.             if (xData.status == 200)  
  50.             {  
  51.                 alert('success');  
  52.             }  
  53.             else  
  54.             {  
  55.                 alert(xData.responseXML.xml);  
  56.             }  
  57.         }  
  58.     });  
  59.     loaditems();  
  60. }  
  61. </script>  
  62. <div id="divHtml">  
  63.     <ul id="tasksUL"></ul>  
  64. </div>  
  65. <table>  
  66.     <tr>  
  67.         <th colspan="2">SharePoint</th>  
  68.     </tr>  
  69.     <tr>  
  70.         <td>ID:</td>  
  71.         <td><input type="text" id="txtid" /></td>  
  72.     </tr>  
  73.     <tr>  
  74.         <td>Title:</td>  
  75.         <td><input type="text" id="txtTitle" /></td>  
  76.     </tr>  
  77.     <tr>  
  78.         <td>Phone:</td>  
  79.         <td><input type="text" id="txtPhone" /></td>  
  80.     </tr>  
  81.     <tr>  
  82.         <td></td>  
  83.         <td><input type="button" value="Update" id="btnsp" onclick="updateListContent();" /></td>  
  84.     </tr>  
  85. </table>  
Step 5: Enter the ID in to the ID text field, Title into title field and Phone into phone field.

Enter detail

Step 6: Click on update. It will update the 5th item and display updated table.

Now you can enjoy the updated program as you want. You can design the page however you want. It can be implemented in CEWP also. 

Up Next
    Ebook Download
    View all
    Learn
    View all