Search code examples
sharepointmicrosoft-graph-apisharepoint-onlineazure-app-registration

Graph API returns 0 drives for a SharePoint Team Site, on which there is a shared folder and its subfolders with files


Context

  • I have an Entra ID user "myuser", who can successfully browse list and view folders and files on a SharePoint Team Site called "mysite" using a browser

  • I have an ASP.NET Core web application (with Azure App Registration), in which the user can log in with his Entra ID user "myuser". The web app successfully acquires access token to the graph api, and successfully can get "mysite" site data including the site id.

Issue

  • The in response of site data the Drives is null.

  • If I try send to direct request to get the site's drives, I also get zero drives

      var siteUrl = "<...>.sharepoint.com";
      var sitePath = "/sites/<...>";
    
      // Get the site ID, 
      // this is success, however site.Drives is null
      var site = await _graphClient.Sites[$"{siteUrl}:{sitePath}"].GetAsync();
      var siteId = site!.Id; // I can get the site id
      // returns HTTP 200, but still with zero drive
      var drives = await _graphClient.Sites[site!.Id].Drives.GetAsync();
    

My ultimate goal to upload a file to a folder in this Team Site, however first I suppose I must get the drive id, which I can not.

Additional information:

I get the _graphClient in the following way:

"myuser" logs in to the my Web Application using SSO via Microsoft login. The Program.cs I am using:

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
  .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
  .EnableTokenAcquisitionToCallDownstreamApi()
  .AddMicrosoftGraph(builder.Configuration.GetSection("GraphV1"))

...then I get graphClient via DI in my controllers.


Solution

  • To fetch the site and drive details you need to grant Sites.Read.All delegated API permission to the Microsoft Entra ID application:

    enter image description here

    And make use of below code to fetch Site and Drive ID:

            try
            {
                var siteUrl = "Tenant.sharepoint.com:";
                var sitePath = "RukTeamSiteTest";
    
                var site = await graphClient.Sites[$"{siteUrl}:{sitePath}"].GetAsync();
    
                Console.WriteLine($"Site ID: {site.Id}");
                Console.WriteLine($"Site Name: {site.Name}");
                Console.WriteLine($"Site Display Name: {site.DisplayName}");
                Console.WriteLine($"Web URL: {site.WebUrl}");
    
                var drives = await graphClient.Sites[site.Id].Drives.GetAsync();
    
                if (drives.Value.Count > 0)
                {
                    foreach (var drive in drives.Value)
                    {
                        Console.WriteLine($"Drive ID: {drive.Id}");
                        Console.WriteLine($"Drive Name: {drive.Name}");
                    }
                }
                else
                {
                    Console.WriteLine("No drives found for this site.");
                }
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
    

    enter image description here