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 !");




  1. 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.


    1. 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!!

  2. 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.

  3. Hi Avanish,

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

  4. 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.

  5. 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!

  6. Hi,

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



Please Login and comment to get your questions answered!

Powered by Blogger.