0

I am trying to expand all items of MS Teams drive (SharePoint document library) using the Graph API.

I can access the team (group) but am unable to directly expand the Items collection. It would be great if that would be possible, so I don't need another query for that (performance!).

According to the documentation there is the $expand= option, in C# I have the Expand() extension method. Unfortunately I cannot find any working example of how to use it. Well, there is one example in the official documentation:

 string messageId = "<guid>";
 Message message = await graphClient.Me.Messages[messageId]
            .Request()
            .Expand("attachments")
            .GetAsync();

But I am unable to reproduce it successfully. The property should be "Items", so I guess I should use lowercase "items" (but I also tried it with "Items").

Here is the teamsDrive, when I won't expand anything, you see the properties is "Null" (it should have items, documents are present in the library):

teamDrive
{Microsoft.Graph.Drive}
Activities [IDriveActivitiesCollectionPage]:null
AdditionalData [IDictionary]:Count = 3
Bundles [IDriveBundlesCollectionPage]:null
CreatedBy:{Microsoft.Graph.IdentitySet}
CreatedByUser [User]:null
CreatedDateTime:{30/05/2020 23:46:46 +00:00}
Description [string]:""
DriveType [string]:"documentLibrary"
ETag [string]:null
Following [IDriveFollowingCollectionPage]:null
Id [string]:"b!QXf1ydGT80WWzfKB0A5IvAkI2isDR2NEh9jgSseDzmFpXz0eAriVS6TXSBEiZhVP"
Items [IDriveItemsCollectionPage]:null
LastModifiedBy [IdentitySet]:null
LastModifiedByUser [User]:null

So, I tried the following:

var teamDrive = await graphClient.Groups[group.Id].Drive.Request().Expand("items").GetAsync();

But this gives me an exception:

Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: invalidRequest
Message: The request is malformed or incorrect.
Inner error:
    AdditionalData:
    request-id: 93ab7efd-0a33-4ff0-800e-d7e95acd8eb4
    date: 2020-06-17T08:41:39
ClientRequestId: 93ab7efd-0a33-4ff0-800e-d7e95acd8eb4

   at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)

I am using the BETA Graph endpoint (https://graph.microsoft.com using an access token retrieved from https://login.microsoftonline.com/{our tenant Id}) with the Microsoft.Graph.Beta NuGet Package (Version 0.18.0-preview - the latest version).

Note that I don't use any select or filter statements in this request, as others have mentioned that this is not allowed when expanding: Expanding and Filtering MS Graph API Does Not Work

Patric
  • 2,789
  • 9
  • 33
  • 60
  • I stumbled upon https://stackoverflow.com/questions/55940857/getting-the-drives-items-from-microsoft-graph-api-the-request-is-malformed-or as I also had a bad request when accessing .Items directly in a separate request. So the trick seems to be to use .Root.Children and not .Items - will check this deeper and maybe post it as an answer. – Patric Jun 17 '20 at 09:25

1 Answers1

0

OK, I found out what was wrong in my request.

The Items collection can only be used, when I want to access a single item. When I want to access the item collection (all items of the collection) I have to expand the Root property of the drive. This Root property then has a Children property containing the items inside the root folder of the drive (note: it doesn't contain all items of document library, only the items of the top level folder).

So basically that means, that I can use:

var teamDrive = await graphClient.Groups[group.Id].Drive.Request().Expand(d => d.Root).GetAsync();

And then can iterate over the children with: foreach(var childItem in teamDrive.Root.Children) { ... }

The following threads helped in finding the solution:

Patric
  • 2,789
  • 9
  • 33
  • 60