In the previous article, we had a look at the “Create” operations in this series of CRUD operations in SharePoint using REST API. In this write-up, we will have a look at the “Read” operations.
Let us 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 |
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
The 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 source, with an 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 given below.
- 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);
The next blog outlines the “Update” operation of REST API in SharePoint. This blog can also be found in my personal blogsite – SharePoint Surgeon
Happy reading!