CRUD Operations in SharePoint using REST API – Part I
Applicable to: SharePoint 2013, SharePoint 2016, SharePoint Online
REST API is a powerful CSOM concept that can be leveraged in SharePoint for converting sophisticated business designs into working and efficient SharePoint Artifacts (Add-Ins, CSOM solutions, etc.). REST does not require any separate references for its working unlike other frameworks (SPservices)
This series of blogs will demonstrate how to work with SharePoint lists and REST API for basic and common operations. Please have a look at MSDN articles if you wish to gain a more in-depth understanding of REST in SharePoint.
REST Operations in SharePoint – CREATE
Step1: Create a custom list “Employee” as per the schema below. Capture the generated internal names of the columns – you will need it later on.
List Name: Employee
Column Name | Internal Name | Type |
Name | Title | Single line of text |
Employee ID | Employee_x0020_ID | Number |
Department | Department | Single line of text |
City | City | Single line of text |
Contact Number | Contact_x0020_Number | Single line of text |
![](https://i0.wp.com/www.sharepointsurgeon.com/wp-content/uploads/2017/10/CreateRest_1-300x124.png?resize=624%2C258)
This is how the list looks like in my case
Step2: Create a webpart page, and insert the list “Employee” in any of the webpart zones.
Step 3: Next step is the addition of the code. The code can be downloaded from – here and the page will look like our case, we will be creating a single page HTML file (i.e. the style, scripts and the body elements will be in a single page, with internal reference to jQuery.min.js file). The HTML can be added to our page in either of the below ways. For this add a Content Editor Web Part (CEWP) in any of the webpart zones and then –
- Insert the code directly in the CEWP.
- Upload the HTML and JS file to any document library, and then insert the link to the HTML file in the Content Link as shown below –
This concludes the steps and the page will be now functional with the “Create” Operation.
Explanatory Step: Although this is not a step, but gives a very brief description of the REST API code used in this scenario. The code used is –
var listName = "Employee"; var itemType = GetItemTypeForListName(listName); function CreateListItem( ) { var name = $("#txtName").val(); var employeeID = $("#txtEmployeeID").val(); var department = $("#slctDepartment").val(); var city = $("#txtCity").val(); var contactNumber = $("#txtContactNumber").val(); var item = { "__metadata": { "type": itemType }, "Title": name, "Employee_x0020_ID": employeeID, "Department": department, "City": city, "Contact_x0020_Number": contactNumber }; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", type: "POST", contentType: "application/json;odata=verbose", data: JSON.stringify(item), headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() }, success: function (data) { alert("success"); }, error: function (data) { alert("success"); } }); } function GetItemTypeForListName (name) { return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem"; }
The function GetItemTypeForListName() returns a string, which is used in the metadata of the list item type that is to be added. The returned string in this case is “SP.DataEmployeeListItem”, which is then in turn used as –
__metadata: {‘type’ : ‘SP.DataEmployeeListItem’ }
As our task here is to create/add data in SharePoint entity (list in this case), the operation is of type/method – “POST”
type: "POST",
And finally, all the data is passed as JSON,
data: JSON.stringify(item),
The next blog outlines the “Read” operation of REST API in SharePoint.
Happy reading!
3,977 total views, 1 views today
5 Comments
Vipin · April 25, 2018 at 5:45 PM
when i am using REST creating URL _spPageContextInfo.webAbsoluteUrl gives Undefined
Satyajit Paul · May 15, 2018 at 2:22 PM
Can you share some more details? What is the exact URL that you’re trying to access?
CRUD Operations in SharePoint using REST API - Part III - SharePoint Surgeon · November 14, 2017 at 1:06 PM
[…] the previous blogs, we had a look at the “Create” and “Read” operations in this series of CRUD Operations in SharePoint using REST API. In this […]
CRUD Operations in SharePoint using REST API - Part II · November 14, 2017 at 1:11 PM
[…] the previous blog, we had a look at the “Create” operations in this series of CRUD Operations in SharePoint using REST API. In this blog, we will […]
CRUD Operations in SharePoint using REST API - Part IV - SharePoint Surgeon · November 14, 2017 at 1:35 PM
[…] previous blogs demonstrates the “Create”, “Read” and “Update” operations of REST API in SharePoint. This is the last blog of this […]