In Focus

Update SharePoint List Items Using REST API

In this blog, I would like to share the code to update the SharePoint list item using SharePoint Rest API.

Here is the detailed documentation for the SharePoint list items and their features.

There are multiple ways to update the list item, here I am sharing the REST API Code

Steps to update the SP List Item

Follow the below listed steps to update an item.

Step 1

Create one JS file or you can use the Content Editor Web Part.

Step 2

Below is the API to get the particular list item using Item ID, you can browse this URL in the browser to check whether the API is working or not.

https://sharepoint.com/_api/web/lists/GetByTitle('samplelist')/items/getbyid(1)

Note

Replace your list name with “samplelist”.

Step 3

Refer to the jQuery in your HTML file or CEWP.

Use the below code to update the item in a SharePoint list.

  1. 'use strict';  
  2. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");  
  3.   
  4. function initializePage() {  
  5.     var siteURL;  
  6.     var itemsArray = [];  
  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.         var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";  
  10.         UpdateListItem();  
  11.     });  
  12.     //Retrieve list items from sharepoint using API  
  13.     function UpdateListItem() {  
  14.         siteURL = _spPageContextInfo.webAbsoluteUrl;  
  15.         console.log("from top nav - " + siteURL);  
  16.         var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items/getbyid(1)";  
  17.         $.ajax({  
  18.                 url: apiPath,  
  19.                 type: "POST",  
  20.                 headers: {  
  21.                     Accept: "application/json;odata=verbose"  
  22.                 },  
  23.                 data: "{__metadata:{'type':'SP.Data.YourlistnameListItem'},Title:”Ur input "  
  24.             }  
  25.             ",  
  26.             async: false, success: function(data) {  
  27.                 alert("Item updated successfully");  
  28.             }, eror: function(data) {  
  29.                 console.log("An error occurred. Please try again.");  
  30.             }  
  31.         });  
  32. }  
  33. }  

 

Summary

In this blog, we have explored how to update a particular item in the SharePoint list using REST API. Happy coding.