Forum Discussion

Sean_Archer's avatar
Sean_Archer
Copper Contributor
May 03, 2023
Solved

Sharepoint Rest API only returning results for single folder

We have an integration that uses the Sharepoint REST API to Create and Query files and folders in a Sharepoint Online document library.    The integration creates a folder structure whenever a new ...
  • Anonymous's avatar
    Anonymous
    May 12, 2023

    Sean_Archer 

    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

Resources