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("https://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 !");
}
}
}
}
}
- If you are looking for a way to upload files to SharePoint List or Library using PowerShell, Have a look at my another post: Upload File to SharePoint Library using PowerShell
- If you want to upload Multiple Files in bulk E..g Upload all files from a local folder to SharePoint, Refer: Bulk upload files to SharePoint using PowerShell
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?
Thanks,
DC
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.
Here is the code you are looking for: Download All Files From a SharePoint Library Programmatically using PowerShell
Regards,
Salaudeen
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.
Thanks
Hi Avanish,
You can use STSADM command line to Deploy InfoPath Forms.
More info: https://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 https://infopathformsinstall.codeplex.com, It does the same! calls STSADM programmatically!!