microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-files

Upload a small file to Sharepoint using Microsoft Graph .NET SDK 5.0


I'm trying to rewrite this code so that it will work with the version 5.0 of the Microsoft Graph .NET SDK:

private async static Task<DriveItem> UploadSmallFile(string driveId, string filePath, MemoryStream fileStream)
{
  var queryOptions = new List<QueryOption>()
  {
    new QueryOption("@microsoft.graph.conflictBehavior", "replace"),
  };

  DriveItem file = await GraphClient
    .Drives[driveId]
    .Root
    .ItemWithPath(filePath)
    .Content
    .Request(queryOptions)
    .PutAsync<DriveItem>(fileStream);
    return file;
}

The following code creates a file but does not return the DriveItem since the PutAsync as a void function (Task). I'm also unclear about how I can pass the conflictBehavior as "replace".

await GraphClient
.Drives[driveId]
.Root
.ItemWithPath(filePath)
.Content
.PutAsync(fileStream);

Any help is appriciated.


Solution

  • I was facing the same issue in the version 5.0.0 and I make one extra call to get new drive item

    var driveItem = await GraphClient.Drives[driveId].Root.ItemWithPath(filePath).GetAsync();
    

    The issue with conflictBehavior is reported here