create/insert new records to a sharepoint list using REST API

 Create/insert new records to a sharepoint list:


To create/add new records in a SharePoint list using REST API, we need these thngs.
1) List Name (Company Details) and its Entity Type Full Name
2) Data that we need to add in list and internal name of Field (Contact Name) and id if it is a lookupfield (Company)

var postData = {

__metadata: {type: "SP.Data.Company_x0020_DetailsListItem"},// List Item Entity TypeFullName

CompanyId: 3, //lookup field id

Contact_x0020_Name: "Himanshu Shekhar",//Text Field

Package_x0020_Status: "Open", // Choice field

Plan_x0020_Holder: true

};


      var listEndPoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Company%20Details')/items"

        $.ajax({

          url: listEndPoint,

          method: "POST",

           async:false, 

            headers: {  

                "accept": "application/json;odata=verbose",  

                "X-RequestDigest": $("#__REQUESTDIGEST").val(),  

                "content-Type": "application/json;odata=verbose"  

            },  

            data: JSON.stringify(postData), 

          success: function (data) {

           console.log(data);

            console.log('Data added successfully');

          },

          error: function (error) {

            // error handler code goes here

            console.log(error)

          }

        });


Output:

Data (CompanyId, Contact Name, Package Status and Plan Holder has updated to server/list)


Console:


Data added successfully;



Was this helpful?



Comments

Popular posts from this blog

Get list item entity type full name of a SharePoint list using REST API

Get SharePoint list name by GUID

Get Current Web Logged In User of SharePoint Site in jQuery