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:
- 'use strict';
- ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
-
- function initializePage() {
- var siteURL;
- var itemsArray = [];
-
- $(document).ready(function() {
- var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
- GetSampleListItems();
- });
-
- function GetSampleListItems() {
- siteURL = _spPageContextInfo.siteAbsoluteUrl;
- console.log("from top nav - " + siteURL);
- var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items";
- $.ajax({
- url: apiPath,
- headers: {
- Accept: "application/json;odata=verbose"
- },
- async: false,
- success: function(data) {
- var items;
- var results;
- if (data != null) {
- items = data.d;
- if (items != null) {
- results = items.results
- for (var i = 0; i < results.length; i++) {
- itemsArray.push({
- "Title": results[i].Title
- });
- }
- }
- }
- },
- eror: function(data) {
- console.log("An error occurred. Please try again.");
- }
- });
- }
- }
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.
| | | | | | | | | |
Text-to-speech function is limited to 200 characters