Share via

Conserve text format when uploading document (.docx) to SharePoint

Kerry Mo 20 Reputation points Microsoft Employee
2024-11-26T04:09:01.34+00:00

Hi all, I'm looking at these

  1. https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#upload-bytes-to-the-upload-session
  2. https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp

Questions:

  • What are the differences between using API vs SDK?
  • Is there a way to conserve the formatting when uploading as docx? Any sample input?
Microsoft 365 and Office | SharePoint | Development
Microsoft Security | Microsoft Graph

Answer accepted by question author

Anonymous
2024-11-26T08:14:20.59+00:00

Hello Kerry Mo,

Thank you for reaching out to Microsoft Support!

1.SDK is an enhancement to the API. The Microsoft Graph software development kits (SDKs) are designed to simplify building high-quality, efficient, resilient applications that access Microsoft Graph. The SDKs include two components: a service library and a core library. See the documentation for more information about the SDK.

2.Upload file code example:

The code to upload a small file is relatively simple.

var fileName = "smallfile.txt";  
var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);  
Console.WriteLine("Uploading file: " + fileName);  
  
FileStream fileStream = new FileStream(filePath, FileMode.Open);  
var uploadedFile = client.Me.Drive.Root  
                              .ItemWithPath("smallfile.txt")  
                              .Content  
                              .Request()  
                              .PutAsync<DriveItem>(fileStream)  
                              .Result;  
Console.WriteLine("File uploaded to: " + uploadedFile.WebUrl);  

The code to upload a larger file requires chunking.

var fileName = "largefile.zip";  
var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);  
Console.WriteLine("Uploading file: " + fileName);  
  
// load resource as a stream  
using (Stream stream = new FileStream(filePath, FileMode.Open))  
{  
    var uploadSession = client.Me.Drive.Root  
                                    .ItemWithPath(fileName)  
                                    .CreateUploadSession()  
                                    .Request()  
                                    .PostAsync()  
                                    .Result;  
  
    // create upload task  
    var maxChunkSize = 320 * 1024;  
    var largeUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, stream, maxChunkSize);  
  
    // create progress implementation  
    IProgress<long> uploadProgress = new Progress<long>(uploadBytes =>  
    {  
        Console.WriteLine($"Uploaded {uploadBytes} bytes of {stream.Length} bytes");  
    });  
  
    // upload file  
    UploadResult<DriveItem> uploadResult = largeUploadTask.UploadAsync(uploadProgress).Result;  
    if (uploadResult.UploadSucceeded)  
    {  
        Console.WriteLine("File uploaded to user's OneDrive root folder.");  
    }  
}  

There are also code examples in the official reference documentation:

https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp

Hope this helps.

If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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