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.
Step 3
Now select yes in Make “New Folder” command available? as shown in the below screenshot.
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 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,
- var ListName = "Test";
- var FolderName = "TestFolder";
- var requestBody = JSON.stringify({
- query: {
- ViewXml: "",
- FolderServerRelativeUrl: site + `/Lists/` + ListName +`/` + FolderName,
- },
- });
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,
- var url = site url + `/_api/Web/Lists/getByTitle('` + ListName +`')/getItems?$select=*,FileDirRef&$orderby=SortOrder,Title asc`;
- fetch(url, {
- method: "POST",
- credentials: "same-origin",
- headers: {
- Accept: "application/json",
- "Content-Type": "application/json;odata=verbose",
- "X-RequestDigest": requestDigest,
- },
- body: requestBody,
- })
- .then((response) => {
- return response.json();
- })
- .then((response) => {
- return resolve(response.value);
- })
- .catch((error) => {
- return reject(error);
- });
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!