CRUD Operations In SharePoint Using REST API - Update Operations

In the previous blogs, we had a look at the “Create” and “Read” operations in this series of CRUD Operations in SharePoint using REST API. In this blog, we will explain the “Update” operations.

As before, let us start with the assumption that the list in picture “Employee” has some entries in it. We will be reading the list based on the ID of the list item – you enter the ID and get the corresponding list item details in HTML table. Next, you modify the values you wish to change and the “post” changes back into the list.

REST Operations in SharePointUPDATE


Create a custom list “Employee” as per the schema below. Capture the generated internal names of the columns – you will need it later on.

List Name - Employee

Column NameInternal NameType
NameTitleSingle line of text
Employee IDEmployee_x0020_ID
Department DepartmentSingle line of text
City CitySingle line of text
Contact NumberContact_x0020_NumberSingle line of text

This is how the list looks like in my case.

Create a web part page, and insert the list “Employee” in any of the web part zones.

Step 3

Next step is the addition of the code. The code can be downloaded from – here. In our case, we will be creating a single page HTML file (i.e. the style, scripts and the body elements will be on a single page, with an internal reference to jQuery.min.js file). The HTML can be added to our page in either of the below ways. For this scenario, we have added a Content Editor Web Part (CEWP) in any of the web part zones and then,

  • Insert the code directly in the CEWP.
  • Upload the HTML and JS file to any document library, and then insert the link to the HTML file in the Content Link as shown below,


Step 4

Enter the ID you wish to update for, and click on “Read” button first. The result will be displayed.


Step 5

Now, make the necessary changes, and click on “Update” button. The change in the list item can be observed now. This concludes the step. Have a look at the below Step 6 to understand the code.


Explanatory Step

This step gives a description of the REST API code used in this scenario. The code used is given below.

  1. var listName = "Employee";  
  2. var itemType = GetItemTypeForListName(listName);  
  4. function GetItemTypeForListName(name)  
  5. {  
  6.             return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";  
  7.         }  
  9. function UpdateListItem()  
  10.         {  
  11.             var myID = $("#numID").val();  
  12.             var name = $("#txtName").val();                
  13.             var employeeID = $("#txtEmployeeID").val();  
  14.             var department = $("#slctDepartment").val();  
  15.             var city = $("#txtCity").val();   
  16.             var contactNumber = $("#txtContactNumber").val();            
  18.             var item = {  
  19.                 "__metadata": { "type": itemType },  
  20.                 "Title": name,  
  21.                 "Employee_x0020_ID": employeeID,  
  22.                 "Department": department,                      
  23.                 "City": city,  
  24.                 "Contact_x0020_Number": contactNumber  
  25.             };  
  27.             $.ajax({  
  28.                 url:  _spPageContextInfo.webAbsoluteUrl  + "/_api/web/lists/getbytitle('" + listName + "')/items(" + myID + ")",  
  29.                 type: "POST",  
  30.                 contentType: "application/json;odata=verbose",  
  31.                 data: JSON.stringify(item),  
  32.                 headers: {  
  33.                     "Accept""application/json;odata=verbose",  
  34.                     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  35.                     "IF-MATCH""*",  
  36.                     "X-HTTP-Method":"MERGE",  
  37.                 },  
  38.                 success: function (data) {  
  39.                     alert("success");  
  40.                 },  
  41.                 error: function (data) {  
  42.                     alert("failed");  
  43.                 }  
  44.             });  
  45.         }  

The function GetItemTypeForListName() returns a string, which is used in the metadata of the list item type that is to be added. The returned string in this particular case is “SP.DataEmployeeListItem”, which is ultimately used in the code line –

__metadata: {“type” : itemType }

As our task here is to update the data in the SharePoint entity (list in this case), the operation is of type/method - POST”.

type: "POST",

The success block alerts a “success” string, after which, a page refresh takes place, and the updated data can be seen in the list item. 

The next blog outlines the “Delete” operation of REST API in SharePoint.

Happy reading!

Up Next
    Ebook Download
    View all
    View all