3

I am trying to get a drive's items via the Microsoft Graph Api (SDK) and tried the following options:

  1. _graphServiceClient.Drives[driveInfo.Id].Items.Request().GetAsync():, this unfortunately results in an error with message error with message "The request is malformed or incorrect" and code "invalidRequest". If I execute _graphServiceClient.Drives[driveInfo.Id].Request().GetAsync() however, I get back all drives but the Items property is null.

  2. _graphServiceClient.Drives[driveInfo.Id].Request().Expand(d => d.Items).GetAsync(), this also results in an error with message "The request is malformed or incorrect" and code "invalidRequest".

I don't know how to go on from here, still researching, but the documentation is leaving me clueless at the moment. Anyone success with either .Expand() or getting the actual files from a Drive?

Thanks, Y

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Yves Schelpe
  • 3,343
  • 4
  • 36
  • 69

1 Answers1

11

You only use Items when you're fetching a single DriveItem:

await graphClient
  .Me
  .Drive
  .Items[item.Id] 
  .Request()
  .GetAsync();

await graphClient
  .Drives[drive.Id]
  .Items[item.Id]
  .Request()
  .GetAsync();

When you want to retrieve a DriveItem collection, you need to specify the root folder:

await graphClient
  .Me
  .Drive
  .Root // <-- this is the root of the drive itself
  .Children // <-- this is the DriveItem collection
  .Request()
  .GetAsync();

await graphClient
  .Drives[drive.Id]
  .Root 
  .Children
  .Request()
  .GetAsync();

The SDK unit tests are a good source for quick examples. For example, OneDriveTests.cs contains several examples for addressing Drives and DriveItems.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • Thanks a lot, this clarifies a lot! Got it working, excellent! – Yves Schelpe May 01 '19 at 20:49
  • 2
    The OneDriveTests.cs file seems to have moved to here now: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/tests/Microsoft.Graph.DotnetCore.Test/Requests/Functional/OneDriveTests.cs (the link at the bottom of the answer 404s). However, your answer has enabled me to finally retrieve a drive item - so thank you very much! – Dan Roberts Oct 12 '20 at 12:25