CRUD Operations in SharePoint using REST API – Part II
Applicable to: SharePoint 2013, SharePoint 2016, SharePoint Online
In 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 explain the “Read” operations.
We start with the assumption that the list in picture “Employee” has some entries in it. We will be reading the list based on the ID of the list item – you enter the ID and get the corresponding list item details in HTML table.
REST Operations in SharePoint – READ
You can skip Step 1 – Step 3 and read directly through the Explanatory Step at the end of this article, if you have read the previous blog. If you haven’t then please read on.
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 |
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. In 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 “Read” Operation.
You will have to enter the ID in the search box and the result will be displayed.
Explanatory Step: This step gives a brief description of the REST API code used in this scenario. The code used is –
function ReadListItem() { var myID = $("#numID").val(); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + myID + ")", method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function (data) { $("#txtName").val(data.d.Title); $("#txtEmployeeID").val(data.d.Employee_x0020_ID); $("#slctDepartment").val(data.d.Department); $("#txtCity").val(data.d.City); $("#txtContactNumber").val(data.d.Contact_x0020_Number); }, error: function (data) { alert("No Item found"); $("#txtName").val(""); $("#txtEmployeeID").val(""); $("#slctDepartment").val(""); $("#txtCity").val(""); $("#txtContactNumber").val(""); } }); }
As our task here is to get/read data in SharePoint entity (list in this case), the operation is of type/method – “GET”
method: "GET",
The success block returns the data we are looking for as per the ID. The individual attributes are then captured from the “data” object
$("#txtName").val(data.d.Title); $("#txtEmployeeID").val(data.d.Employee_x0020_ID); // and so on..
The next blog outlines the “Update” operation of REST API in SharePoint.
Happy reading!
2,012 total views, 1 views today
7 Comments
Vipin · April 26, 2018 at 12:06 PM
url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/lists/getbytitle(‘” + listName + “‘)/items(” + myID + “)”,
gives Error
https://site Url/sites/ Undefined + “/_api/web/lists/getbytitle(‘” + listName + “‘)/items(” + myID + “)”,
how can i resolve it
Satyajit Paul · May 15, 2018 at 2:24 PM
What is the error message? And what browser/SharePoint version are you using?
vipin · May 15, 2018 at 2:49 PM
error inside URL (Undefined) SharePoint version 2010 and browser IE, Firefox, Chrome
Jannet Fernandes · January 16, 2020 at 11:06 AM
Hi. Can you tell how can we read the data into the form without typing the ID every time? Can we just click on the hyperlink and open the display form with data?
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 blog, we will […]
CRUD Operations in SharePoint using REST API -Part I · November 14, 2017 at 1:20 PM
[…] next blog outlines the “Read” operation of REST API in […]
CRUD Operations in SharePoint using REST API - Part IV - SharePoint Surgeon · November 14, 2017 at 1:52 PM
[…] previous blogs demonstrates the “Create”, “Read” and “Update” operations of REST API in SharePoint. This is the last blog of this series and […]