microsoft-graph-apisharepoint-online

Graph API v5 upload file to sharepoint


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


Solution

  • 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