How To Get List Item From Particular Folder Of SharePoint List Using Rest API

Introduction

 
In this blog, we will learn how can we get list items from a particular folder of SharePoint list using Rest API. You can create a folder in the list if you have enabled the setting to allow you to create folders.
 

How to enable create folder settings

 
You can create a folder in the list when you have enabled folder creation from list settings. Follow the below steps to enable this setting,
 
Step 1
 
To allow create folders in the list, go to list settings.
 
Step 2
 
Now go to advanced settings.
 
How To Get List Item From Particular Folder Of SharePoint List Using Rest API
 
Step 3
 
Now select yes in Make “New Folder” command available? as shown in the below screenshot.
 
How To Get List Item From Particular Folder Of SharePoint List Using Rest API
 
Step 4
 
Click on the OK button. Now in a list, you can see the option to create a folder when clicking on the New button.
 
How To Get List Item From Particular Folder Of SharePoint List Using Rest API
 
How to get list items from folder
 
To get the data from a particular folder, we need to use POST requests instead of GET requests.
 
Also, we will pass the query as an in the body of the post request.
 
We need to specify the query as below,
  1. var ListName = "Test";  
  2. var FolderName = "TestFolder";  
  3. var requestBody = JSON.stringify({  
  4.           query: {  
  5.             ViewXml: "",  
  6.             FolderServerRelativeUrl: site + `/Lists/` + ListName +`/` + FolderName,  
  7.           },  
  8.         });  
Now to get items from TestFolder of Test list, we will use fetch request as below. As we are using POST request, we will also need request digest,
  1. var url = site url +   `/_api/Web/Lists/getByTitle('` + ListName +`')/getItems?$select=*,FileDirRef&$orderby=SortOrder,Title asc`;            
  2. fetch(url, {  
  3.    method: "POST",  
  4.    credentials: "same-origin",  
  5.    headers: {  
  6.         Accept: "application/json",  
  7.         "Content-Type""application/json;odata=verbose",  
  8.         "X-RequestDigest": requestDigest,  
  9.    },  
  10.     body: requestBody,  
  11.  })  
  12.  .then((response) => {  
  13.      return response.json();  
  14.  })  
  15.   .then((response) => {  
  16.      return resolve(response.value);  
  17.  })  
  18.  .catch((error) => {  
  19.      return reject(error);  
  20.  });  

Summary

 
This is how we can get the list items from a particular folder of the list. Hope this blog will be helpful to you!