2

I have installed the Graph API v5 using c# and I am trying to follow the below examples of uploading a file to sharepoint. Seems like some of the methods do not exist anymore in the graph API v5.

1st code doesnt recognise Items

var uploadSession = await graphClient.Drive.Items["itemId"].ItemWithPath("SWEBOK.pdf").CreateUploadSession().Request().PostAsync();

doesnt recognise Root

var uploadedFile = client.Me.Drive.Root  
                              .ItemWithPath("smallfile.txt")  
                              .Content  
                              .Request()  
                              .PutAsync<DriveItem>(fileStream)  
                              .Result;  

Graph API documentation

Upload example

Anyone can refer me to documentation of how to upload using graph api v5.

Is it better to use a older version of Graph API instead of the version 5. Also is it better to use http client call instead of using the SDK libraries. Thanks

JasonK
  • 23
  • 3

2 Answers2

2

If you know driveId then you can access MicrosoftGraphCreateUploadSession this way

var uploadSessionBody = new Microsoft.Graph.Drives.Item.Items.Item.MicrosoftGraphCreateUploadSession.CreateUploadSessionPostRequestBody
{
    // Item = ...
};
var uploadSession = await _client.Drives["driveId"]
                             .Items["itemId"]
                             .ItemWithPath("SWEBOK.pdf")                
                             .MicrosoftGraphCreateUploadSession
                             .PostAsync(uploadSessionBody);

Upload file to your Root

var uploadedFile = await _client.Drives["Me"].Root
                          .ItemWithPath("smallfile.txt")
                          .Content
                          .PutAsync(fileStream);

Doc:

Upgrade guide to v5

user2250152
  • 14,658
  • 4
  • 33
  • 57
  • Thanks was able to upload using suggested code. Although cannot get return file id of uploaded file as method doesnt return value. await graphClient.Drives[drive.Id].Items[driveItem.Id].ItemWithPath("file.txt").Content.PutAsync(fileStream) – JasonK Mar 02 '23 at 23:14
  • @JasonK Yes, it returns only Task. You need to make one extra call var newDriveItem = await graphClient.Drives[drive.Id].Items[driveItem.Id].ItemWithPath("file.txt").GetAsync(); Hope it will be fixed soon in SDK – user2250152 Mar 03 '23 at 06:11
0

This process is documented at "Upload large files using the Microsoft Graph SDKs".

First of, configure a CreateUploadSessionPostRequestBody

// Use properties to specify the conflict behavior
// in this case, replace
var uploadSessionRequestBody = new CreateUploadSessionPostRequestBody
{
    Item = new DriveItemUploadableProperties
    {
        AdditionalData = new Dictionary<string, object>
        {
            { "@microsoft.graph.conflictBehavior", "replace" }
        }
    }
};

Note that this class is apparently present in numerous namespaces. In my case the correct one was Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession, but YMMV.

Possible locations of CreateUploadSessionPostRequestBody

I needed to upload files to a specific directory, and thus used this code:

var uploadSession = await GraphServiceClient
    .Drives[driveId]
    .Items[folderId]
    .ItemWithPath(fileName)
    .CreateUploadSession
    .PostAsync(uploadSessionRequestBody);

If it is a large file, you need to do this in chunks:

int maxSliceSize = 320 * 1024 * 33; // ~10MB
var fileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxSliceSize);

And then you need to execute the upload:

var uploadResult = await fileUploadTask.UploadAsync(progress);

For completion, here are the usings at the top of my class which contains this code:

using Microsoft.Graph;
using Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35