Forum Discussion
Sharepoint Rest API only returning results for single folder
- AnonymousMay 12, 2023
you can execute a CAML query against the results obtained from the `GetFolderByServerRelativeUrl` endpoint to achieve a RecursiveAll query and retrieve all files below a specified folder. However, the SharePoint REST API doesn't directly support CAML queries on folders. Instead, you can recursively call the `GetFolderByServerRelativeUrl` endpoint to retrieve files and subfolders.
Here's an example of how you can recursively retrieve files and folders using PowerShell and the SharePoint REST API:
private static void GetFilesAndFoldersRecursive(string folderServerRelativeUrl) { string filesUrl = $"{_sapItemsUrl}GetFolderByServerRelativeUrl('{folderServerRelativeUrl}')/Files"; string foldersUrl = $"{_sapItemsUrl}GetFolderByServerRelativeUrl('{folderServerRelativeUrl}')/Folders"; // Retrieve files in the current folder string filesResponse = SendGetRequest(filesUrl); // Process files... // Retrieve subfolders in the current folder string foldersResponse = SendGetRequest(foldersUrl); JsonNode data = JsonNode.Parse(foldersResponse); JsonArray foldersArray = data["value"].AsArray(); foreach (JsonNode folderNode in foldersArray) { string subfolderServerRelativeUrl = folderNode["ServerRelativeUrl"].AsString(); // Recursively call the method for each subfolder GetFilesAndFoldersRecursive(subfolderServerRelativeUrl); } } private static string SendGetRequest(string url) { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add("Authorization", $"Bearer {BearerToken}"); HttpResponseMessage response = client.GetAsync(url).Result; response.EnsureSuccessStatusCode(); return response.Content.ReadAsStringAsync().Result; } }In the above code, the `GetFilesAndFoldersRecursive` method takes the server-relative URL of a folder as a parameter. It first retrieves the files within the current folder using the `GetFolderByServerRelativeUrl` endpoint with the `/Files` suffix. Next, it retrieves the subfolders using the same endpoint with the `/Folders` suffix. It then processes the files in the current folder and recursively calls itself for each subfolder, passing the server-relative URL of the subfolder.
You can modify and integrate this code into your existing solution to retrieve all files below a specified folder in a recursive manner. Make sure to provide the correct server-relative URL of the starting folder when calling the `GetFilesAndFoldersRecursive` method.
Note: This example uses the `HttpClient` class for making HTTP requests. Ensure that you have the necessary namespaces imported and handle the disposal of the `HttpClient` instance appropriately in your actual implementation.
By recursively traversing the folder structure, you can retrieve all files and folders below a specified folder in SharePoint using the SharePoint REST API.
If I have answered your question, please mark your post as SolvedIf you like my response, please give it a like
Hi Deleted
I really appreciate your continued input and suggestions, thank you.
Unfortunately I need to use the REST API and cannot use the Sharepoint.Online.Client library in my solution.
I had a working solution using the Sharepoint.Online.Client, but when deployed to the target environment my library is being called by another executable which is in turn being called by the SAP application. In short there was a conflict between the DLL dependencies that I was not able to work around.
Currently the issues I have with the rest API are:
- <site>/api/web/lists/GetByTitle('Documents')/GetItems fails due to the number of items in the list. Even when specifying the FolderServerRelativeUrl parameter
- <site>/api/web/lists/GetByTitle('Documents')/RenderListAsDataStream works but for some reason, only for a single item
- <site>/api/web/GetFolderByServerRelativePath(<itempath>)/ListItemAllFields returns the top level information but I cannot figure out how to provide a query to return the files recursively below the specified folder.
Cheers,
Sean
you can execute a CAML query against the results obtained from the `GetFolderByServerRelativeUrl` endpoint to achieve a RecursiveAll query and retrieve all files below a specified folder. However, the SharePoint REST API doesn't directly support CAML queries on folders. Instead, you can recursively call the `GetFolderByServerRelativeUrl` endpoint to retrieve files and subfolders.
Here's an example of how you can recursively retrieve files and folders using PowerShell and the SharePoint REST API:
private static void GetFilesAndFoldersRecursive(string folderServerRelativeUrl)
{
string filesUrl = $"{_sapItemsUrl}GetFolderByServerRelativeUrl('{folderServerRelativeUrl}')/Files";
string foldersUrl = $"{_sapItemsUrl}GetFolderByServerRelativeUrl('{folderServerRelativeUrl}')/Folders";
// Retrieve files in the current folder
string filesResponse = SendGetRequest(filesUrl);
// Process files...
// Retrieve subfolders in the current folder
string foldersResponse = SendGetRequest(foldersUrl);
JsonNode data = JsonNode.Parse(foldersResponse);
JsonArray foldersArray = data["value"].AsArray();
foreach (JsonNode folderNode in foldersArray)
{
string subfolderServerRelativeUrl = folderNode["ServerRelativeUrl"].AsString();
// Recursively call the method for each subfolder
GetFilesAndFoldersRecursive(subfolderServerRelativeUrl);
}
}
private static string SendGetRequest(string url)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {BearerToken}");
HttpResponseMessage response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsStringAsync().Result;
}
}In the above code, the `GetFilesAndFoldersRecursive` method takes the server-relative URL of a folder as a parameter. It first retrieves the files within the current folder using the `GetFolderByServerRelativeUrl` endpoint with the `/Files` suffix. Next, it retrieves the subfolders using the same endpoint with the `/Folders` suffix. It then processes the files in the current folder and recursively calls itself for each subfolder, passing the server-relative URL of the subfolder.
You can modify and integrate this code into your existing solution to retrieve all files below a specified folder in a recursive manner. Make sure to provide the correct server-relative URL of the starting folder when calling the `GetFilesAndFoldersRecursive` method.
Note: This example uses the `HttpClient` class for making HTTP requests. Ensure that you have the necessary namespaces imported and handle the disposal of the `HttpClient` instance appropriately in your actual implementation.
By recursively traversing the folder structure, you can retrieve all files and folders below a specified folder in SharePoint using the SharePoint REST API.
If I have answered your question, please mark your post as Solved If you like my response, please give it a like |