Search code examples
c#microsoft-graph-apionedrive

I want to download the content of a DriveItem , how do I get the driveItem's Id?


I am trying to get a DriveItem by the Item Id, however when I attempt this, I get an error saying the no item matching the Id I gave was found. I am using the MSGraph .NET SDK.

var items = await GraphClient.Me.Drive.Special.AppRoot.Children.Request().GetAsync();
var files = items.CurrentPage;
foreach (var file in files)
{
     if (file.File.MimeType == "application/json" && filename == file.Name)
     {
        var itemId = file.Id;                   
        return await GraphClient.Me.Drive.Special.AppRoot.Children[itemId].Request().GetAsync();
     }
}

Solution

  • The children collection expects a file name as the key. Since you have the item id (from file.Id) you want to use it with the items collection:

    ...
    return await GraphClient.Me.Drive.Items[itemId].Request().GetAsync();
    ...