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

Issue

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