Learn how to perform CRUD (Create, Read, Update and Delete) operations using REST API in SharePoint List. You can use it for SharePoint Online, 2010, 2013 and 2016.
.
Setup required
We are using a SharePoint custom list named "EmployeeDetails" with below columns:
Column Name | Type |
EmloyeeName (internal name is Title) | Single line of text |
Address | Multiple lines of text |
Country | Choice (India, US, UK) |
Age | Number |
Salary | Currency |
DateOfBirth | Date and Time |
IsEmployeeMarried | Checkbox |
ManagerName | Person or group |
ProfileUrl | Hyperlink |
Download: You can download the template of this SharePoint list here.
Create Item:
//Create a SharePoint List Item using REST Api function createItem() { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('EmployeeDetails')/Items", type: 'POST', headers: { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "X-HTTP-Method": "POST" }, data: JSON.stringify({ __metadata: { type: "SP.Data.EmployeeDetailsListItem" }, Title: "Mayuresh Dinkar Joshi", Address: "ABC Center, Near Sdftji Dfrtg 123123 Test", Country: 'India', Age: 25, Salary: 1000, DateOfBirth: "1993-09-21T07:00:00Z", IsEmployeeMarried: true, ManagerNameId: 16, //internalName is 'ManagerName' but we have to use 'ManagerNameId' to pass id of a person ProfileUrl: { __metadata: { "type": "SP.FieldUrlValue" }, Url: "http://test.com", Description: "Url Description" }, }), success: function(data) { alert('Item created successfully!'); }, error: function(error) { alert(JSON.stringify(error)); } }); }
Read Item:
//Read a SharePoint List Item using REST Api function readItem() { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('EmployeeDetails')/Items(1)", type: 'GET', headers: { "accept": "application/json;odata=verbose", }, success: function(data) { console.log(data.d); alert(" First name: " + data.d.Title + "\n Address: " + data.d.Address + "\n Country: " + data.d.Country + "\n Age: " + data.d.Age + "\n Salary: " + data.d.Salary + "\n DateOfBirth: " + data.d.DateOfBirth + "\n IsEmployeeMarried: " + data.d.IsEmployeeMarried + "\n ManagerName: " + data.d.ManagerNameId + "\n ProfileUrl: " + data.d.ProfileUrl.Url); }, error: function(error) { alert(JSON.stringify(error)); } }); }
Update Item
//Update a SharePoint List Item using REST Api function updateItem() { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('EmployeeDetails')/Items(1)", type: "PATCH", headers: { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "IF-MATCH": "*", "X-HTTP-Method": "MERGE" }, data: JSON.stringify({ __metadata: { type: "SP.Data.EmployeeDetailsListItem" }, Title: "updated Mayuresh Dinkar Joshi", Address: "updated ABC Center, Near Sdftji Dfrtg 123123 Test", Country: 'US', Age: 26, Salary: 2000, DateOfBirth: "1993-09-22T07:00:00Z", IsEmployeeMarried: false, ManagerNameId: 16, //internalName is 'ManagerName' but we have to use 'ManagerNameId' to pass id of a person ProfileUrl: { __metadata: { "type": "SP.FieldUrlValue" }, Url: "http://test1.com", Description: "updated Url Description" }, }), success: function(data) { alert('Item updated successfully!'); }, error: function(error) { alert(JSON.stringify(error)); } }) }
Delete Item
//Delete a SharePoint List Item using REST Api function deleteItem() { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('EmployeeDetails')/Items(1)", type: 'DELETE', headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(), "IF-MATCH": "*", "X-HTTP-Method": "DELETE" }, success: function(data) { alert('Item deleted'); }, error: function(error) { alert(JSON.stringify(error)); } }) }
0 Comments