Maximilian K.
Maximilian K.

Reputation: 227

Microsoft Graph List OneDrive Items

I want to list all my OneDrive for Business Items in an UWP-Application. For this I'm using "Microsoft Graph Client Library" from NuGet. It's a wrapper Library for the Microsoft Graph REST-API.

When I'm trying to get all items or children (I tried both) from my root-drive or from an spezific Folder-ID, I just get an empty List. But there are different Files and Folders in my Drive. Even when I'm using the REST-API without this wrapper, I'm getting just an empty Result.

But when I'm using the "Recent" function, I'm getting a list of my recent used Items.

// Returns an empty result without error
GraphServiceClient.Me.Drive.Items.Request().GetAsync()
GraphServiceClient.Me.Drive.Root.Children.Request().GetAsync()
GraphServiceClient.Drives["id"].Items.Request().GetAsync()

// Returns all my recent used items
GraphServiceClient.Me.Drive.Recent().Request().GetAsync()
GraphServiceClient.Drives["id"].Recent().Request().GetAsync()

The HTTP-Traffic Looks like:

GET https://graph.microsoft.com/v1.0/me/drive/root/children HTTP/1.1
SdkVersion: graph-dotnet-1.0.1
Cache-Control: no-store, no-cache
Authorization: Bearer 1234567890123456789
Host: graph.microsoft.com
Connection: Keep-Alive

// Response:

HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
Server: Microsoft-IIS/8.5
request-id: 123456-7890123
client-request-id: 123456-7890123
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"West Europe","Slice":"SliceB","ScaleUnit":"000","Host":"AGSFE_IN_3","ADSiteName":"AMS"}}
OData-Version: 4.0
Duration: 823.6454
X-Powered-By: ASP.NET
Date: Wed, 15 Jun 2016 06:56:29 GMT

8c
{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('123456-7890123-456789')/drive/root/children","value":[]}
0

(I've removed the Id's, so thats not the Problem)

Can someone help?

Upvotes: 2

Views: 5418

Answers (2)

Daniel W.
Daniel W.

Reputation: 1198

With the new Verion 5 of the Graph API I was stocked on getting listing all files, so I want to share my solution. If you know the folder name you want to list you may use AddFolderFromOne directly. As on the root folder the things work diffrent I added a function AddFromRoot for the root folder.

public async Task<IList<(String name, String id)>> GetAllFilesFromOne()
{
    IList<(String name, String id)> results = new List<(String name, String id)>();
    try
    {
        var drives = await _graphClient.Me.Drives.GetAsync();
        await AddFilesFromRoot(drives.Value[0], results);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error on List Files");
    }
    return results;
}

// Get the Folders and Files on 
private async Task AddFilesFromRoot(Drive rootDrive, IList<(string name, string id)> results)
{
    // On this level is is required to use a filter to avoid error 'The 'filter' query option must be provided.'
    var rootItems = await _graphClient.Drives[rootDrive.Id].Root.GetAsync((conf) =>
    {
        conf.QueryParameters.Expand = new string[] { "children" };
    });

    foreach (var child in rootItems.Children)
    {
        if (child.Folder != null)
        {
            // Add recursive the files from a subfolder 
            AddFolderFromOne(rootDrive, results, $"{child.Name}");
        }
        else
        {
            results.Add(($"{child.Name}", child.Id));
        }
    }
}

// Add recursive the files from a subfolder 
private async Task AddFolderFromOne(Drive rootDrive, IList<(string name, string id)> results, String itemPath)
{
    var children = _graphClient.Drives[rootDrive.Id].Root.ItemWithPath(itemPath);
    var childs = await children.Children.GetAsync();

    if (childs != null)
    {
        foreach (var child in childs.Value)
        {
            if (child.Folder != null)
            {
                AddFolderFromOne(rootDrive, results, $"{itemPath}/{child.Name}");
            }
            else
            {
                results.Add(($"{itemPath}/{child.Name}", child.Id));
            }
        }
    }
}

Upvotes: 0

Fei Xue
Fei Xue

Reputation: 14649

To get the items from OneDrive using the Microsoft Graph, we need to make the request to the endpoint. The ‘Request’ method didn’t make the real request until we call ‘GetAsync’ method.

Here is an example that get the children items of the default drive:

var items = await graphserviceClient.Me.Drive.Root.Children.Request().GetAsync();

Refer to here for more detail about this SDK.

Upvotes: 2

Related Questions