Search code examples
c#sharepointuploadmicrosoft-graph-apimicrosoft-graph-sdks

Graph API SharePoint site file upload: complile error DriveItemRequestBuilder does not contain a definition for Root


I am trying to upload file to a SharePoint Site's shared folder using the following code:

var uploadSession = await _graphClient.Sites[siteId]
   .Drives[driveId]
   .Root
   .ItemWithPath($"{folderPath}{info.Name}")
   .CreateUploadSession()
   .Request()
   .PostAsync();

I got compile error:

'DriveItemRequestBuilder' does not contain a definition for 'Root' and no accessible extension method 'Root' accepting a first argument of type 'DriveItemRequestBuilder' could be found (are you missing a using directive or an assembly reference?)

I've also tried to use .Items["root"] instead of .Root, same error

Further thoughts:

  • I realized that this is because some breaking changes in Microsoft.Graph nuget package, with version 4.x.x it seems to be compile at least. However I must use the 5.x.x package for other code parts.
  • To make finding the answer even more hopeless I realized that there are two different DriveItemRequestBuilder class, one in the namespace Microsoft.Graph.Drives.Item.DriveItemRequestBuilder this is for the personal folder like var uploadSession = await _graphClient.Drives[myDrive?.Id] and a completely different in Microsoft.Graph.Sites.Item.Drives.Item.DriveItemRequestBuilder this is for _graphClient.Sites[siteId].Drive... this is for the Sites, and what I need.

Literally all examples are either for the legacy version, or the personal folder...


Solution

  • Code for SDK v5 is autogenerated and generating of C# code for some paths like /sites/{siteId}/drives/{driveId}/items/* is restricted.

    With a knowledge of driveId, you can directly call /drives/{driveId} to access the root folder and an item by a path

    var uploadSession = await _graphClient
    .Drives[driveId]
    .Root
    .ItemWithPath($"{folderPath}{info.Name}")
    .CreateUploadSession
    .PostAsync(new Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession.CreateUploadSessionPostRequestBody { });