Retrieve SharePoint List Items Using Rest API

In this blog, I would like to share the code to get the SharePoint list items using SharePoint Rest API. We have already seen how to retrieve the item from SharePoint List using JSOM object model

Here is the detailed documentation for the list and the features.

Steps to retrieve the SharePoint List items using rest API

Follow the below-listed steps to retrieve the items

Step 1

Create one JS file or you can use the content editor web part.

Step 2

Below is the API to get the list items, you can browse this URL in the browser to check whether the API is working or not:

https://sharepoint.com/_api/web/lists/GetByTitle('samplelist')/items

Note

Replace your list name in “samplelist.”

Step 3

Refer to the jQuery in your HTML file or content editor web part.

Use the below code to retrieve the item from SharePoint lists:

  1. 'use strict';  
  2. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");  
  3.   
  4. function initializePage() {  
  5.     var siteURL;  
  6.     var itemsArray = [];  
  7.     // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  8.     $(document).ready(function() {  
  9.         var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";  
  10.         GetSampleListItems();  
  11.     });  
  12.     //Retrieve list items from sharepoint using API  
  13.     function GetSampleListItems() {  
  14.         siteURL = _spPageContextInfo.siteAbsoluteUrl;  
  15.         console.log("from top nav - " + siteURL);  
  16.         var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items";  
  17.         $.ajax({  
  18.             url: apiPath,  
  19.             headers: {  
  20.                 Accept: "application/json;odata=verbose"  
  21.             },  
  22.             async: false,  
  23.             success: function(data) {  
  24.                 var items; // Data will have user object  
  25.                 var results;  
  26.                 if (data != null) {  
  27.                     items = data.d;  
  28.                     if (items != null) {  
  29.                         results = items.results  
  30.                         for (var i = 0; i < results.length; i++) {  
  31.                             itemsArray.push({  
  32.                                 "Title": results[i].Title  
  33.                             });  
  34.                         }  
  35.                     }  
  36.                 }  
  37.             },  
  38.             eror: function(data) {  
  39.                 console.log("An error occurred. Please try again.");  
  40.             }  
  41.         });  
  42.     }  
  43. }  

Rest API performance is fast compared to JSOM object model.

Using API, we can retrieve the particular column values and we can do the filtering, ordering, and sorting.

Refer to the below image for more options:

Summary

In this blog, we have explored how to retrieve the list items from SharePoint using REST API.

Happy coding.