How to Programmatically Upload File to SharePoint Document Library?

Code snippet to programmatically upload Files to SharePoint List or Library:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Collections;

namespace UploadFiles
    class Program
      static void Main(string[] args)
          //Get the Site collection
            using (SPSite site = new SPSite(""))
                //Open the Root web
                using(SPWeb web=site.OpenWeb())
                /*** Simple File Upload ********/
                // Read the file from the stream into the byte array
                FileStream fs= File.OpenRead(@"c:\MyDoc.doc");
                byte[] FileContent= new byte[fs.Length];
                fs.Read(FileContent, 0, Convert.ToInt32(fs.Length));
                //Get the documents Librarym named "Documents"
                SPList DocLib = web.Lists["Documents"];
                //Add the file
                web.Files.Add(DocLib.RootFolder + "/MyDoc.doc", FileContent, true);

                /*** If you want to add inside a folder: say "Sales" *******/
                // Get the folder called "Sales"
                SPFolder SubFolder = DocLib.RootFolder.SubFolders["Sales"];
                //Add the file to the sub-folder
                SPFile file = SubFolder.Files.Add(SubFolder.Url + "/MyFile.doc", FileContent, true);

                //IF you want to Update the Meta data column say "Country"
                SPListItem item = DocLib.Items[file.UniqueId];
                item["Country"] = "Denmark";
                // OR We can use the Hash Table
                var Metadata = new Hashtable { { "Country", "India" } };
                // Add the file
                web.Files.Add(DocLib.RootFolder + "/MyDoc2.doc", FileContent, Metadata, true);

                /**** Check-in the file, If the Library has mandatory Columns or Explicit Check-out Enabled *******/
                if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                    file.CheckIn("File uploaded Programmatically !");

                /**** Publish the file using File.publish() method If Minor versions enabled! ****/
                //file.Publish("File published Programmatically !");



Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

9 thoughts on “How to Programmatically Upload File to SharePoint Document Library?

  • Hi there, I am trying to implement this in Console application
    Add all code in new solution, but can see some compile time errors at “SPSite”, “SPWeb”, “SPList”,”SPFolder”, “SPFile”, “SPListItem” locations in code.

    Did I miss anything. Please advise me.

  • Hi,

    I need same for deleting the file, can you please help?


  • Hey there! I’ve been following your website for some time now and
    finally got the bravery to go ahead and give you a shout out from Humble
    Tx! Just wanted to say keep up the great job!

  • Can you also please tell how to download files from SharePoint library? Its very urgent.
    I have a code with me, but that all of a sudden has started a weird problem.
    The problem is, after downloding the file, its getting corrupt.
    Can you please help/ suggest any workaround?
    Thanks in advance.

  • Hi Avanish,

    How would you disable an item created alert if you wanted to bulk upload?

  • Thanks for reply and help,
    The stsadm command line is useful but can i publish my new InfoPath form in a particular form library which i have created by code on sharepoint site.
    Because I saw that with stsadm we are giving only the sie url and Infopath form’s path.

  • HI Salaudeen,
    Nice blog!
    Could you please tell me how can i publish InfoPath form in Form library by C# code as same as we publish by publishing wizard, I have seen the methods for uploading the InfoPath form in Library but I want to publish my InfoPath form in Form library by programmatically instead of uploading it.


    • Hi Avanish,

      You can use STSADM command line to Deploy InfoPath Forms.
      More info:

      If you want to make it programmatically, just call stsadm from your code.

      There is a Utility in Codeplex, Along with Source code, It does the same! calls STSADM programmatically!!


Leave a Reply

Your email address will not be published. Required fields are marked *