Search code examples
c#microsoft-graph-apinuget-package

Migration of the “Microsoft Graph” Nuget package from V4.5 to V5


For a C# project, I use the “Microsoft Graph” Nuget package. Although my project worked fine with version 4.5 of this package, migrating to version 5 causes many compilation errors And the help on the Internet does not provide any detailed solutions.

Example of lines that compiled and no longer compile:

await GraphClient.Me.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync()

Error CS1061 'DriveRequestBuilder' does not contain a definition for 'Items' and no accessible extension method 'Items' accepting a first argument of type 'DriveRequestBuilder' was found (a using directive or assembly reference is is it missing?)

await GraphClient.Me.Drive.Items[item_id].CreateLink(type, scope).Request().PostAsync()

'DriveRequestBuilder' does not contain a definition for 'Items' and no accessible extension method 'Items' was found that accepts a first argument of type 'DriveRequestBuilder' (is a using directive or assembly reference missing?)

By consulting the Internet:

  • I read that “Me.Drive” should be replaced by: Drives[userDriveId]. By doing the sequence:
var driveItem = await client.Me.Drive.GetAsync();
// For example :
var children = await client.Drives[driveItem.Id].Root.Children.GetAsync();
  • I also read that “Special.AppRoot” was replaced: Special["approot"]

  • Finally, the “Microsoft Graph .NET SDK v5 changelog and upgrade guide” says that you must replace “Request().GetAsync()” with “GetAsync()”

Following these recommendations, if I replace my line which has an error with:

await GraphClient.Drives[driveItem.Id].Special["approot"].ItemWithPath(filepath).Content.GetAsync()

I get the error:

CS1929: 'DriveItemItemRequestBuilder' does not contain a definition for 'ItemWithPath' and the best extension method overload 'DriveItemRequestBuilderExtensions.ItemWithPath(RootRequestBuilder, string)' requires a receiver of type 'Microsoft.Graph.Drives.Item.Root.RootRequestBuilder'

I am trying to migrate my code which used version V4.5 of “Microsoft Graph” to version V5 of this package but my attempts are not working.


Solution

  • The following page https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2035 gives the solution to the problem.

    The functional code is therefore:

    Drive driveItem = await GraphClient.Me.Drive.GetAsync();
    DriveItem appRootFolder = await GraphClient.Drives[driveItem.Id].Special["AppRoot"].GetAsync();
    Stream downloaded_file = await GraphClient.Drives[driveItem.Id].Items[appRootFolder.Id].ItemWithPath(filepath).Content.GetAsync();