upload file to sharePoint Library Programmatically
Programmatically upload files to document library in SharePoint

Hello ShareaPointers,

Today, In this article we will learn about How to upload a File into SharePoint Document Library using C# Code or how to Programmatically Upload a File in SharePoint Document Library.

We implemented this solution long back ago because our client requires to automate bulk upload documents. Hope in the latest technology changing world you will find this article helpful.

Please Note: This solution or Piece of code only applies to SharePoint On-premises versions Like MOSS 2007, SharePoint 2010, and SharePoint 2013.


Upload files into SharePoint Library Programmatically using C#


Let us assume we already have a site and other variable properties as below.

  • SiteUrl: https://SharePointforfun.com
  • Library Name: Documents
  • File Name: TestDoc.doc
  • Country: India
  • Sub Folder Name: IT

That’s it for the Prerequisites, and now let us move ahead with the actual code.

C# Program to upload the file into SharePoint Document Library.

Step 1. Declare assemblies and Namespace: Declare assemblies from SharePoint as per below code snippet.

Using System;
Using System.SiteCollections.Generic;
Using System.Linq;
Using System.Text;
Using Microsoft.SharePoint; 
Using System.IO;
Using System.Collections;

Namespace UploadFiles
   Class Program
      //your code goes here

Step 2. Access your Site collection in code.

//Retrive sitecollection
using(SPSite site = new SPSite ("https://spforfun.com"))
  //Open Web
  using(SPWeb web=site.OpenWeb())
    //Your remaining code goes here....

Step 3. Read a file (which you want to upload) into bytes array using stream functions.

//Read the file using stream into a byte array
FileStream fs = File.OpenRead(@"c:\TestDoc.doc");
byte[] Content = new byte[fs.Length];
fs.Read(Content, 0, Convert.ToInt32(fs.Length));

Step 4. Get the document Library named “Documents”. At this location, your file will be uploaded.

//get library named "Documents"
SPList DocLib = web.Lists["Documents"];

Step 5. Add a file into Library called “Documents”

//Add a File
web.Files.Add(DocLinb.RootFolder + "/TestDoc.doc", Content, true);

In this way, you can upload a file into SharePoint document Library.

Now Lets go ahead and do some more stuff on this.

Upload a File into Sub-folder of Document Library.

Suppose we have a sub-folder named IT in our “Documents” Library. And we want to add our file into it, and below is the code used to add a file into sub-folder.

//Get Sub-folder of Library named "IT"
SPFolder subFolder = DocLib.RootFolder.SubFolders["IT"];
//Add a file into the sub-Folder.
SPFile file = subFolder.Files.Add(subFolder.URL, + "/TestFile.doc", Content, true);

Update Meta Data of file programmatically

Let us update a metadata Property, Let’s say “Country” columns value we need to update.

//update metadata column Country
SPListItem item = DocLib.Items[file.UniqueId];
item["Country"] = "India";

//OR We can also Use method called Hash Table
var Metadata = new Hashtable{{"Country", "India"}};
//add a file now
web.Files.Add(DocLib.RootFolder + "/TestDoc2.doc", FileContent, Metadata, true);

Check In a file if Library has Mandatory columns.

if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
   file.CheckIn("File uploaded Successfully!");

Publish a file : Please NOTE- only publish a file when minor versions are enabled.

//file.Publish("File published Successfully!");

Full code

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 your Site collection
            using (SPSite site = new SPSite("http://spforfun.com"))
                //Open Root web
                using(SPWeb web=site.OpenWeb())
                // Read the file from the stream into the byte array
                FileStream fs= File.OpenRead(@"c:\TestDocDoc.doc");
                byte[] Content= new byte[fs.Length];
                fs.Read(Content, 0, Convert.ToInt32(fs.Length));
                //Get the documents Librarym named "Documents"
                SPList DocLib = web.Lists["Documents"];
                //Add the file
                web.Files.Add(DocLib.RootFolder + "/TestDoc.doc", Content, true);
                /*** If you want to add inside a folder: say "IT" *******/
                // Get the folder called "IT"
                SPFolder SubFolder = DocLib.RootFolder.SubFolders["IT"];
                //Add the file to the sub-folder
                SPFile file = SubFolder.Files.Add(SubFolder.Url + "/NewFile.doc", Content, true);
                //IF you want to Update the Meta data column say "Country"
                SPListItem item = DocLib.Items[file.UniqueId];
                item["Country"] = "India";
                // OR We can use the Hash Table
                var Metadata = new Hashtable { { "Country", "India" } };
                // Add the file
                web.Files.Add(DocLib.RootFolder + "/TestDoc2.doc", Content, Metadata, true);
                if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                    file.CheckIn("File uploaded Successfully !");
                //file.Publish("File published Successfully !");

I hope you liked this (Programmatically Upload a File in SharePoint Document Library) article, and if you think this is useful to you then please hit a Like button or express your feelings with comment.

9 thoughts on “Programmatically Upload a File in SharePoint Document Library.”
  1. Thanks for the interesting things you have revealed in your blog post. One thing I would really like to discuss is that FSBO interactions are built eventually. By presenting yourself to the owners the first saturday their FSBO is usually announced, ahead of the masses commence calling on Monday, you produce a good interconnection. By giving them tools, educational materials, free reports, and forms, you become a good ally. By taking a personal desire for them and their scenario, you generate a solid relationship that, many times, pays off as soon as the owners opt with a real estate agent they know plus trust – preferably you actually.

  2. I’m the proprietor of JustCBD Store company (justcbdstore.com) and I’m presently looking to develop my wholesale side of business. I am hoping someone at targetdomain share some guidance . I thought that the most ideal way to accomplish this would be to reach out to vape stores and cbd retailers. I was really hoping if someone could recommend a reputable web-site where I can get CBD Shops Business Marketing Data I am currently looking at creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. Not sure which one would be the most ideal selection and would appreciate any support on this. Or would it be easier for me to scrape my own leads? Suggestions?

Leave a Reply