How To Work With List Items In SPFx Using REST API - Retrieve List Data - Part One

SharePoint Framework is a powerful client-side development tool used to develop SharePoint apps with full support of other Javascript frameworks like Angular, React, Knockout.

SharePoint Framework is a powerful client-side development tool used to develop SharePoint apps with the full support of most of the other JavaScript frameworks like Angular, React, Knockout…

Generally, in order to access the SharePoint REST API call request, we would be in need of authentication parameters like access tokens, Request Digest value, etc.

But in SPFx, we have no need to worry about that because SPFx takes care of that authentication by using the current user’s context in SPHttpClient class. The only thing we need to do is to pass the URL with some headers and post body.

We can accomplish all the list operations by using the “Get” and “Post” methods with the below options.

  • url
    SP Rest API url to access your data

  • configuration
    SPHttpClientConfiguration which uses predefined objects required for SPHttpClient class

  • options
    ISPHttpClientOptions which is optional in the GET request and required in POST request. With this parameter, we can pass custom headers and payload to post.

Let’s see real examples to retrieve data from the list with the help of spHttpClientClass. Below are the basic request URLs required to read list data.

  • GetListItemCount
    To get the count of the items in a list
    “/_api/web/Lists/GetByTitle('YourListTitle')/ItemCount”

  • GetListItems
    To get the count of the items in a list
    /_api/web/Lists/GetByTitle('YourListTitle')/items$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles”

    Here OData query is used to read the attachment files in the list in a single call while reading the list items.

  • GetListItemsWithCAMLQuery
    To get the count of the items in a list
    /_api/web/Lists/GetByTitle('YourListTitle')/GetItems$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles”

  • GetListItemByItemId
    To get the count of the items in a list
    /_api/web/Lists/GetByTitle('YourListTitle')/items(YourItemID)$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles”

The first step is to import the below modules in your TypeScript file from the package “@microsoft/sp-http” like below.

  1. import {  
  2.     SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions} from '@microsoft/sp-http';  

Next, to get the web absolute URL where the SPFx app is running -

  1. let currentWebUrl = this.context.pageContext.web.absoluteUrl;  
Get Items count in a list (Get operation is used)
  1. let requestUrl = currentWebUrl.concat(“/_api/web/Lists/GetByTitle('YourListTitle')/ItemCount”)   
  2.   
  3. this.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)  
  4.     .then((response: SPHttpClientResponse) => {  
  5.         if (response.ok) {  
  6.             response.json().then((responseJSON) => {  
  7.                 if (responseJSON!=null && responseJSON.value!=null){  
  8.         let itemCount:number = parseInt(responseJSON.value.toString());  
  9.                 }  
  10.             });  
  11.         }  
  12.     });  

Finally, the highlighted line yields you the items count in a list.

Get all Items in a list without using caml query (Get operation is used)
  1. let requestUrl = currentWebUrl.concat(“/_api/web/Lists/GetByTitle('YourListTitle')/items$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles”)   
  2.   
  3. this.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)  
  4.     .then((response: SPHttpClientResponse) => {  
  5.         if (response.ok) {  
  6.             response.json().then((responseJSON) => {  
  7.                 if (responseJSON!=null && responseJSON.value!=null){  
  8.                     let items:any[] = responseJSON.value;  
  9.                 }  
  10.             });  
  11.         }  
  12.     }); 

Finally, the highlighted line gives you the items in a list, but be sure your list doesn’t have more than 5000 items because SharePoint has the limitation that we cannot read more than 5000 items at a time to improve performance. In case you have more than 5000 items in a list you must apply caml query to get the filtered list items. (Note: you have to ensure that caml query also shouldn’t return more than 5000 items) Below is a sample for that also.

Get all Items in a list WITH caml query (Post operation is used)

There are two differences between the request with caml query and without caml query

  1. We will be using POST method to use caml query in request body because if CAML query string length exceeds, we cannot directly pass it in ODATA URL since there is some URL length restriction is available hence we are posting our caml query in the request body.

  2. A slight change in the URL
    • With Query: api/web/Lists/GetByTitle('YourListTitle')/GetItems
    • Without Query: api/web/Lists/GetByTitle('YourListTitle')/items

In SPFx, everything you post in Request Body should have metadata about the body contents, the same as for caml query, as below.

Code Snippet

  1. let camlQueryPayLoad: any = {  
  2.             query: {  
  3.                 __metadata: { type: “SP.CamlQuery” },  
  4.                 ViewXml: query  
  5.             }  
  6.         };  
  7.   
  8.     let spOpts = {                  
  9.                 body: JSON.stringify(camlQueryPayLoad)  
  10.             };  
  11.   
  12.   
  13. this.context.spHttpClient.post(requestUrl, SPHttpClient.configurations.v1, spOpts)  
  14.     .then((response: SPHttpClientResponse) => {  
  15.         if (response.ok) {  
  16.             response.json().then((responseJSON) => {  
  17.                 if (responseJSON!=null && responseJSON.value!=null){  
  18.                     let items:any[] = responseJSON.value;  
  19.                 }  
  20.             });  
  21.         }  
  22.     });  
Get list item by itemId (Get operation is used)
  1. let requestUrl = currentWebUrl.concat(“/_api/web/Lists/GetByTitle('YourListTitle')/items(YourItemID)$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles”)   
  2.   
  3. this.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)  
  4.     .then((response: SPHttpClientResponse) => {  
  5.         if (response.ok) {  
  6.             response.json().then((responseJSON) => {  
  7.             if (responseJSON!=null){  
  8.                 let items:any = responseJSON;  
  9.                 }  
  10.             });  
  11.         }  
  12.     });  

If you are working with SPFx for the first time, please make use of browser console for better understanding, console.log (responseJSON)

Look for for the next part of this blog to learn about how to add/update/delete items from a list.