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("http://sharepoint.com"))
            {
                //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));
                fs.Close();
                //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);
                SubFolder.Update();

                //IF you want to Update the Meta data column say "Country"
                SPListItem item = DocLib.Items[file.UniqueId];
                item["Country"] = "Denmark";
                item.Update();
                // 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

Information Technology Professional with Two decades of SharePoint Experience.

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

  • October 11, 2020 at 9:13 AM

    Hi,

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

    Thanks,
    DC

    Reply
  • December 1, 2017 at 7:38 PM

    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!

    Reply
  • June 24, 2013 at 7:41 AM

    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.

    Reply
  • October 29, 2012 at 8:57 PM

    Hi Avanish,

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

    Reply
  • October 5, 2012 at 1:46 PM

    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.

    Reply
  • October 4, 2012 at 2:38 PM

    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.

    Thanks

    Reply
    • October 4, 2012 at 3:47 PM

      Hi Avanish,

      You can use STSADM command line to Deploy InfoPath Forms.
      More info: http://walisystems.com/articles/SPS/publishusingcmdline/publishing_infopath_web_form_usi.htm

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

      There is a Utility in Codeplex, Along with Source code http://infopathformsinstall.codeplex.com, It does the same! calls STSADM programmatically!!

      Reply

Leave a Reply