Create SharePoint List Items Using REST API

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

Here is the detailed documentation for the lists and their features.

There are multiple ways to create a list item, here I’m sharing the REST API Code.

Steps to Create the SP List Item

Follow the below listed steps to Create 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, 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

Note

Replace your list name with “samplelist”.

Step 3

Refer to the jQuery in your HTML file or CEWP.

Use the below code to Create an item in a SharePoint list.

 

  1. 'use strict';  
  2. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");  
  3.   
  4. function initializePage() {  
  5.     var siteURL;  
  6.     // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  7.     $(document).ready(function() {  
  8.         var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";  
  9.         CreateListItem();  
  10.     });  
  11.     //Retrieve list items from sharepoint using API  
  12.     function CreateListItem() {  
  13.         siteURL = _spPageContextInfo.webAbsoluteUrl;  
  14.         console.log("from top nav - " + siteURL);  
  15.         var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items";  
  16.         $.ajax({  
  17.                 url: apiPath,  
  18.                 type: "POST",  
  19.                 headers: {  
  20.                     Accept: "application/json;odata=verbose"  
  21.                 },  
  22.                 data: "{__metadata:{'type':'SP.Data.YourlistnameListItem'},Title:”Ur input "  
  23.             }  
  24.             ",  
  25.             async: false, success: function(data) {  
  26.                 alert("Item is Created successfully!!");  
  27.             }, eror: function(data) {  
  28.                 console.log("An error occurred. Please try again.");  
  29.             }  
  30.         });  
  31. }  
  32. }  

 

Summary

In this blog, we have explored how to create an item in the SharePoint list using REST API. Happy Coding!!