Sharepoint - using Graph api to get the folders and files for a sharepoint site

Larry Bellou 20 Reputation points
2025-01-13T17:01:09.06+00:00

Hello, I am successfully retrieving a AccessToken via oAuth 2.0 with roles of

"roles": [ "Sites.Selected", "Sites.Read.All", "User.Read.All", "Files.Read.All", "Sites.FullControl.All"

        public void Sharepoint_MicrosoftTest()
        {
            string clientId = "123";
            string tenantId = "123";
            string clientSecret = "123"; // Updated client secret
            var authority = $"https://login.microsoftonline.com/{tenantId}";
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var app = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(new Uri(authority))
                .Build();
            var authResult = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;
            var accessToken = authResult.AccessToken;
            Console.WriteLine($"Access Token: {accessToken}");
            // Use the access token to call Microsoft Graph API
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = httpClient.GetAsync("https://graph.microsoft.com/v1.0/sites/mycompany.sharepoint.com:/sites/SubscriberAcquisitionResourceCenter").Result;
            var content = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine($"Response: {content}");
        }

What i now need to do is list the folders and download all the files for each folder recursively to a local directory using c#. I am having a hard time finding the folders shown in screenshot using Graph.PV_SubscriberAcquisitionResourceCenter

when I run the graph expression i would expect to use ,

GET https://graph.microsoft.com/v1.0/sites/mycompany.sharepoint.com:/sites/SubscriberAcquisitionResourceCenter

i get back an id successfully. The id has companysharepoint,guid,guid looks like.

then use that

https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/Resource%20Center:/children

I get back access denied. I have tried other things as well but not sure what permissions to use (if that is even the issue)..

attached are permissions set in Graph, temporarily added as many as I could think of..

User's image

Tried figuring it out in https://developer.microsoft.com/en-us/graph/graph-explorer as well without any luck. Thanks for your help!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,778 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,192 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 22,536 Reputation points Microsoft Vendor
    2025-01-14T06:37:40.4633333+00:00

    Hi @Larry Bellou,

    To list the folders and download files recursively from a SharePoint site using the Microsoft Graph API, you need to ensure that you have the correct permissions and are using the right API endpoints.

    1. Permissions: Ensure that your application has the necessary permissions to access the SharePoint site and its contents. The permissions you mentioned (Sites.Selected, Sites.Read.All, Files.Read.All, Sites.FullControl.All) should generally allow you to read files and folders. However, you may need to verify that the permissions are granted for the specific site and that they are not just granted at the tenant level.
    2. API Endpoint for Folders: To retrieve the folders within a specific directory, you can use the following endpoint:
            GET https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/Resource%20Center:/children
      
      Make sure to replace {site-id} with the actual site ID you obtained. The Resource Center should be the correct path to the folder you want to access.
    3. Access Denied Issue: If you are getting an "access denied" error, it could be due to insufficient permissions or because the folder path is incorrect. Double-check that the folder name is correctly encoded in the URL (e.g., spaces should be replaced with %20).
    4. Recursive Download: To download files recursively, you will need to:
      • List the folders using the endpoint mentioned above.
      • For each folder, call the same endpoint to retrieve its children.
      • If a child is a file, download it using:
             GET https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id}/content 
        
      • If a child is a folder, repeat the process.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.