Add new item to SharePoint list using REST API with Dynamic Values

Loading

SharePoint 2013/2016 or in SharePoint online classic pages we can use REST API to add new items in SharePoint list, to add data to list we can use below code.

$(document).ready(function ()
{
  $('#newitem').click()
  {
   NewItem()
}
});
function NewItem()
{
  var path = _spPageContextInfo.webAbsoluteUrl+ "/_api/lists/getbytitle(Listname)/items";
var $name = $('#Textbox').val();
var $Department = 'MSBU';
$.ajax ({
url: path,
type: POST,
headers: {
           Accept: "application/json;odata=verbose",
           "content-type": "application/json;odata=verbose",         
           "X-RequestDigest": $("#__REQUESTDIGEST").val()
         }
      "__metadata": {
      "type": "SP.Data.ListnameListItem"
  },
  "Title": "ItemTitle",
  "Name":$name,
  "Unit": $Department
}
  success: function(data) {  
                alert("Item Added successfully...");  
            }, 
  error: function(data) {  
                alert("An error occurred. Please try again.");  
            }  
});

Leave a Reply

Your email address will not be published. Required fields are marked *