1

Is there a way to recursively include the content of the children (grandchild) in one request in the Microsoft Graph API?

I want to query folder /foo/ and also get the content of /foo/baar/ e.g /foo/baar/baz.txt.

IGraphServiceClient graphClient = GraphServiceClient
   .builder()
   .authenticationProvider(authProvider)
   .buildClient();

IDriveItemCollectionPage children = graphClient
   .drives("{drive-id}")
   .items("{item-id}")
   .children()
   .buildRequest()
   .expand("children")
   .get();

Expanding the query using children returns a com.microsoft.graph.http.GraphFatalServiceException: Unexpected exception returned from the service.Error code: notSupported

This would significantly enhance the performance of my requests.

Edit: just created a feature request: List children including grandchildren

Jules
  • 189
  • 1
  • 5
  • 20

1 Answers1

4

It is not possible with expand to get files more than 1 level. It is mentioned in Known Issues section of Microsoft Graph Api docs.

But we can get all files and folders recursively using search.

Search with q=''(empty query search), this will return all files and folder in the search scope. For scope specific search, refer to this answer. You can use parentReference key to sort out which file belongs to which folder.

Example 1: The following query retrieves all files and folders in the root scope

https://graph.microsoft.com/v1.0/me/drive/root/search(q='')?$select=name,id,parentReference

Example 2: To search in the folder named 'temp' located in root folder

https://graph.microsoft.com/v1.0/me/drive/root:/temp:/search(q='doc')?$select=name,id,parentReference

Limitations with search (as of now)

  • Files and folders that are created recently take some time to reflect in the search results as they need to be indexed. They are visible when you query for children but they won't appear in search results.
  • The empty query trick works only with OneDrive Business accounts but not with OneDrive Personal accounts. For OneDrive personal accounts, when you search with empty query, it returns an error message Search Query cannot be empty"
  • The scope specific search(eg: search within a specific folder) works with OneDrive Personal but not with OneDrive business accounts.
Sriteja Sugoor
  • 574
  • 3
  • 13
  • Using the search is a awesome hint, thanks for that!. If I try the search functionality starting by a specific directory, all items (from all folders) will occur in the result e.g. `/me/drive/items/{id-of-subdirectory}/search(q='')?$select=name,id,parentReferenc`. Do you have also for this problem a hint for me? – Jules Mar 01 '20 at 20:00
  • I didn't understand your problem correctly. You want to search for files in the directory as level 1 and not to get files from level 2? – Sriteja Sugoor Mar 02 '20 at 11:28
  • Thanks again!!! I ran into your third limitation because I tested with a OneDrive business account, thanks for mentioning it...hmm combining all your limitations I've to find another solution :) – Jules Mar 02 '20 at 18:17
  • @Jules have you found any other solution for scanning all files in drive ? – Ronak Dhoot Jul 27 '20 at 10:41
  • @RonakDhoot Unfortunately not yet :/ . – Jules Jul 27 '20 at 11:51
  • In the C# SDK you could do `var allContent = await _graphClient.Drives[{Drive ID}.Root.Search().Request().GetAsync();` – jao Sep 30 '20 at 11:21