CRUD Operations In SharePoint Using REST API - READ Operations

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 NameInternal NameType
NameTitleSingle line of text
Employee IDEmployee_x0020_IDNumber
DepartmentDepartmentSingle line of text
CityCitySingle line of text
Contact NumberContact_x0020_NumberSingle line of text

This is how the list looks like in my case.
SharePoint
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.

SharePoint

SharePoint

SharePoint

Explanatory Step

This step gives a brief description of the REST API code used in this scenario. The code used is given below.

  1. function ReadListItem()    
  2.     {    
  3.         var myID = $("#numID").val();  
  4.         $.ajax({  
  5.             url:  _spPageContextInfo.webAbsoluteUrl  + "/_api/web/lists/getbytitle('" + listName + "')/items(" + myID + ")",  
  6.             method: "GET",  
  7.             headers: { "Accept""application/json; odata=verbose" },  
  8.             success: function (data) {  
  9.   
  10.                 $("#txtName").val(data.d.Title);                
  11.                 $("#txtEmployeeID").val(data.d.Employee_x0020_ID);  
  12.                 $("#slctDepartment").val(data.d.Department);  
  13.                 $("#txtCity").val(data.d.City);   
  14.                 $("#txtContactNumber").val(data.d.Contact_x0020_Number);     
  15.             },  
  16.             error: function (data) {  
  17.                 alert("No Item found");  
  18.                 $("#txtName").val("");                
  19.                 $("#txtEmployeeID").val("");  
  20.                 $("#slctDepartment").val("");  
  21.                 $("#txtCity").val("");   
  22.                 $("#txtContactNumber").val("");    
  23.             }  
  24.             });  
  25.         }  

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.

  1. $("#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!