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
In the code, the `serverRelativeUrl` variable is set to the path of the folder structure for the specified item code. If this path is incorrect or doesn't exist for other items, the REST API call may not return any results.
To troubleshoot this issue, you can try the following:
1. Check that the folder structure for the other items exists in the document library and has the same permissions as the working item.
2. Verify that the `serverRelativeUrl` variable is constructed correctly for each item. You can try logging the value of this variable to the console to confirm that it matches the expected path.
3. Try querying the document library using the SharePoint REST API directly, without the C# code, to see if the issue is with the code or the SharePoint site. You can use a tool like Postman to send HTTP requests to the SharePoint REST API.
4. Check the SharePoint site's logs or monitoring tools to see if any errors or issues are reported when querying for the files.
Overall, it's difficult to determine the exact cause of the issue without more information or debugging. However, checking the folder structure, URL path, and using the SharePoint REST API directly can help pinpoint the issue.
Deleted
Thank you for the suggestions. Please see the comments in Bold below
Deleted wrote:
Based on the provided code, it seems like the issue may be related to the `serverRelativeUrl` variable that's used to construct the REST API URL.
In the code, the `serverRelativeUrl` variable is set to the path of the folder structure for the specified item code. If this path is incorrect or doesn't exist for other items, the REST API call may not return any results.
To troubleshoot this issue, you can try the following:
1. Check that the folder structure for the other items exists in the document library and has the same permissions as the working item.
The file structure for both items is the same and works for other searches. I have also double checked the permissions are the same on both folders and subfolders.
DCW0012-B600-R12_TEST has files but returns no data
DCW0012-B600-114_TEST has files and returns data
2. Verify that the `serverRelativeUrl` variable is constructed correctly for each item. You can try logging the value of this variable to the console to confirm that it matches the expected path.
I have verified this both in my code and in Postman
3. Try querying the document library using the SharePoint REST API directly, without the C# code, to see if the issue is with the code or the SharePoint site. You can use a tool like Postman to send HTTP requests to the SharePoint REST API.
Good Call. Running the query in Postman I get the same results as my application.
For DCW0012-B600-R12_TEST I get the following with no row data
{"Row": [],"FirstRow": 1,"FolderPermissions": "0x7ffffffffffbffff","LastRow": 0,"RowLimit": 30,"FilterLink": "?","ForceNoHierarchy": "1","HierarchyHasIndention": "","CurrentFolderSpItemUrl": ""}
For DCW0012-B600-114_TEST the rows array has the file details{"Row": [{"ID": "3544","PermMask": "0x7fffffffffffffff","FSObjType": "0","HTML_x0020_File_x0020_Type": "","UniqueId": "{F6D1E477-ED9F-492A-920A-C30A8F88A714}","ProgId": "","NoExecute": "1","ContentTypeId": "0x01010041E4CB00DC5F6849AAB5E7D9774C969D",...
4. Check the SharePoint site's logs or monitoring tools to see if any errors or issues are reported when querying for the files.
I'm not sure where to find these so will reach out to the Sites Administrator.
Overall, it's difficult to determine the exact cause of the issue without more information or debugging. However, checking the folder structure, URL path, and using the SharePoint REST API directly can help pinpoint the issue.Yes, it is proving very difficult to pinpoint. The fact that the query is behaving the same way in Postman suggests the issue probably lies more in the Site Configuration or behaviour rather than the code.
One thing to note is that there are no Lists on the site, only the Document Library. I am assuming that the Document Library is treated as a list as we can use the /Lists/GetByTitle('Documents') to return the list and the fact that the query is working for at least 1 item.