I know how to create a word document using c#.

The issue I am having is I don't know how to place that document in the SharePoint folder.

var newfile = System.IO.Path.Combine(
Directory.GetCurrentDirectory(), "Documents", "filename" + ".docx");


We can use Open XML SDK to create Office documents, then using SharePoint client object model File.SaveBinaryDirect Method to upload the document to folder in SharePoint library.

Example code:

string siteUrl = "https://tenant.sharepoint.com/sites/lz";
var login = "lz@tenant.onmicrosoft.com";
var password = "xxx";
var documentLibrary = "DL0627";
var folderUrl = "/sites/lz/"+documentLibrary+"/folder1/";
var fileName = "test1.docx";

using (var stream = new MemoryStream())
    //1. Create Document
    using (var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
        document.MainDocumentPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Some content goes here")))));

    //2. Upload file into a SharePoint Library folder 
    using (var ctx = new ClientContext(siteUrl))
        var securePassword = new SecureString();
        foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
        ctx.Credentials = new SharePointOnlineCredentials(login, securePassword);
        var targetFileUrl = String.Format("{0}/{1}", folderUrl, fileName);
        stream.Position = 0;
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, targetFileUrl, stream, true);

enter image description here

|improve this answer|||||

You can upload documents to SharePoint libraries using the Object Model or SharePoint Webservices.

Upload using Object Model:

String fileToUpload = @"C:\YourFile.txt";
String sharePointSite = "http://yoursite.com/sites/Research/";
String documentLibraryName = "Shared Documents";

using (SPSite oSite = new SPSite(sharePointSite))
    using (SPWeb oWeb = oSite.OpenWeb())
        if (!System.IO.File.Exists(fileToUpload))
            throw new FileNotFoundException("File not found.", fileToUpload);                    

        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

        // Prepare to upload
        Boolean replaceExistingFiles = true;
        String fileName = System.IO.Path.GetFileName(fileToUpload);
        FileStream fileStream = File.OpenRead(fileToUpload);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

        // Commit 


  1. How do you upload a file to a document library in sharepoint?.
  2. C# code to upload document to SharePoint.
  3. Upload A Document To A SharePoint 2013 Library Using Client Side Object Model.
|improve this answer|||||
  • I am using .net Core MVC missing names space for "SPSite," SPFile and SPWeb already reference Microsoft.SharePoint.Client dll and Microsoft.SharePoint dll – Agha Jun 26 '19 at 16:26
  • Check this solution, how to use CSOM to upload document in SharePoint library. – Ganesh Sanap Jun 26 '19 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.