question

kelly-7927 avatar image
0 Votes"
kelly-7927 asked saldana-msft edited

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

I am reading this article to uplaod large files to library in sharepoint site: https://docs.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

sharepoint-devmicrosoft-graph-filesmicrosoft-graph-sites-lists
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered

Hi @kelly-7927,

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.









5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.