Search code examples
c#asp.netgraphmicrosoft-graph-api

Get Folders content using Graph SDK - C#


Actually I'm using Microsoft Graph SDK for get my SharePoint content and upload files.

var driveItems = await _graph.Sites["myCompany.sharepoint.com"]
                             .Drive
                             .Root
                             .Children
                             .Request()
                             .GetAsync();

But using this code snippet, I'm stuck on root folder. Which means I can't get the files located on a folder, or upload a file in his subfolder, for example.

How can I navigate between folders using this Microsoft Graph SDK?


Solution

  • Yes, you can get the list of files and folders by using the code which you shared and I have tested it as below.

    Used the Code :-

    var driveItems = await graphClient.Sites["soaadteam.sharepoint.com,c1178396-d845-46fa-bc0c-453d2951dad5,19ee9a1e-001d-48f1-9ee8-b0adfde54e45"]
                                 .Drive
                                 .Root
                                 .Children
                                 .Request()
                                 .GetAsync();
    
                    foreach(var items in driveItems)
                    {
                        Console.WriteLine(items.Name);
                    }
    

    Result:- A folder and a file.

    The

    The only reason that you are not getting those files could be due to the permissions that you don't have in your scope. Please add Sites.Read.All permission and try to read the results.