Microsoft Graph API Query works on Explorer but not in Microsoft Graph .NET Client Library

Viewed 209

I have a SharePoint Site, and inside a Documents folder, I have an Excel file.I want to make a specific Microsoft Graph call which gets the details of where the file resides. This works as expected when I test the query in Graph Explorer:

https://graph.microsoft.com/v1.0/sites/b8b6d734-2f94-4070-ae22-xxxxxxxxxxxx/drive/root

When I attempt to use the query in a console application using the Microsoft Graph .NET Client library, the test query executes fine:

var resultPage = await graphClient
  .Sites["b8b6d734-2f94-4070-ae22-xxxxxxxxxxxx"]
  .Drive.Root
  .Request()
  .GetAsync();

When I try to select a specific folder inside that root, it also works in Graph Explorer:

https://graph.microsoft.com/v1.0/sites/b8b6d734-2f94-4070-ae22-xxxxxxxxxxxx/drive/root/children/{foldername}/children/

But when I try this same query in my app, it get an error returned.

The code:

var resultPage = await graphClient
  .Sites["b8b6d734-2f94-4070-ae22-xxxxxxxxxxxx"]
  .Drive
  .Root
  .Children["foldername"]
  .Children
  .Request()
  .GetAsync();

The response:

Error getting events: 
  Code: itemNotFound
  Message: The resource could not be found.

I also tried using the folder's id instead of the but that also failed.

1 Answers

If you want it to use a path you need to use ItemWithPath:

var resultPage = await graphClient
  .Sites["b8b6d734-2f94-4070-ae22-xxxxxxxxxxxx"]
  .Drive
  .Root
  .ItemWithPath("/{foldername}")
  .Children
  .Request()
  .GetAsync();

You can also retrieve it via the DriveItem id like this:

var resultPage = await graphClient
  .Sites["b8b6d734-2f94-4070-ae22-xxxxxxxxxxxx"]
  .Drive
  .Items["{item-id}"]
  .Children
  .Request()
  .GetAsync();
Related