Upload large files to SharePoint Online library with graph api using c# code

Kelly 81 Reputation points
2021-11-22T13:30:00.22+00:00

I am reading this article to uplaod large files to library in sharepoint site: https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0.

But there is no C# demo code in the documentation, can anyone provide an example for me?

Thanks in advance
Kelly

Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-11-23T05:56:46.103+00:00

    Hi @Kelly ,

    Please refer to this post: https://stackoverflow.com/questions/49776955/how-to-upload-a-large-document-in-c-sharp-using-the-microsoft-graph-api-rest-cal

    Create upload session

    // Create upload session   
    // POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession  
    var uploadSession = await graphClient.Drive.Items[itemId].ItemWithPath("SWEBOK.pdf").CreateUploadSession().Request().PostAsync();  
    

    Create the task

    // Create task  
    var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.  
    var largeFileUpload = new LargeFileUpload(uploadSession, graphClient, stream, maxChunkSize);  
    

    Create upload monitor

    public class MyProgress : IProgressCallback  
    {  
        public void OnFailure(ClientException clientException)  
        {  
            Console.WriteLine(clientException.Message);  
        }  
      
        public void OnSuccess(DriveItem result)  
        {  
            Console.WriteLine("Download completed with id below");  
            Console.WriteLine(result.Id);  
        }  
      
        public void UpdateProgress(long current, long max)  
        {  
            long percentage = (current * 100) / max ;  
            Console.WriteLine("Upload in progress. " + current + " bytes of " + max + "bytes. " + percentage + " percent complete");  
        }  
    }  
    

    Upload the file

    uploadedFile = await largeFileUpload.ResumeAsync(new MyProgress());  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.