Alternative Save/WriteBinaryDirect methods for CSOM for SharePoint Online

Fabio Brandão 6 Reputation points
2021-03-09T04:52:52.37+00:00

Based on the doc from MS
https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard

12469-csom.png

So for .NET core app, what is the alternative way to upload files to a Document Library on SharePoint Online?
What is the regular file API?
Does anyone done this?
Any example code/documentation?

Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-03-10T02:18:54.147+00:00

    Hi @Fabio Brandão ,

    Below is my sample code to upload files to document library, hope it helps you:

        string filepath = @"C:\temp\test.txt" ;  
        FileCreationInformation newfile = new FileCreationInformation();  
        byte[] FileContent = System.IO.File.ReadAllBytes(filepath);  
        newfile.ContentStream = new MemoryStream(FileContent);  
        newfile.Url = "test.txt";  
        Folder library = context.Web.GetFolderByServerRelativeUrl("/sites/michael/Shared%20Documents");  
        Microsoft.SharePoint.Client.File uploadfile = library.Files.Add(newfile);  
        context.Load(uploadfile);  
        context.ExecuteQuery();  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.


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.