Javascript required
Skip to content Skip to sidebar Skip to footer

Upload Files to Sharepoint Folder Using Power Shell Client Side

Programmatically upload files to document library in SharePoint

Hello ShareaPointers,

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

We implemented this solution long dorsum ago because our client requires to automate bulk upload documents. Promise in the latest engineering irresolute world you volition find this commodity helpful.

Please Note: This solution or Slice of code only applies to SharePoint On-bounds versions Similar MOSS 2007, SharePoint 2010, and SharePoint 2013.

Requirement:

Upload files into SharePoint Library Programmatically using C#

Assumptions:

Let u.s. assume nosotros already take a site and other variable properties every bit beneath.

  • SiteUrl: https://SharePointforfun.com
  • Library Name: Documents
  • File Name: TestDoc.doc
  • Country: Republic of india
  • Sub Binder Name: Information technology

That's it for the Prerequisites, and now allow us move alee 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 Organization; Using Organization.SiteCollections.Generic; Using Organisation.Linq; Using System.Text; Using Microsoft.SharePoint;  Using Arrangement.IO; Using System.Collections;  Namespace UploadFiles {    Class Program     {       //your lawmaking goes here     } }          

Footstep 2. Access your Site collection in lawmaking.

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

Step 3. Read a file (which you desire 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)); fs.close();          

Footstep iv. Go the document Library named "Documents". At this location, your file will be uploaded.

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

Pace five. Add a file into Library called "Documents"

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

In this style, you tin 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 Certificate Library.

Suppose nosotros have a sub-binder named IT in our "Documents" Library. And nosotros want to add our file into information technology, and below is the code used to add together a file into sub-binder.

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

Update Meta Data of file programmatically

Let us update a metadata Property, Let's say "Country" columns value we need to update.

            //update metadata cavalcade Land SPListItem particular = DocLib.Items[file.UniqueId]; item["Country"] = "Bharat"; item.Update();  //OR We can likewise Use method chosen Hash Tabular array var Metadata = new Hashtable{{"Country", "India"}}; //add a file now web.Files.Add together(DocLib.RootFolder + "/TestDoc2.doc", FileContent, Metadata, truthful);                      

Cheque 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 Organisation; 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 Principal(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));                 fs.Close();                 //Get the documents Librarym named "Documents"                 SPList DocLib = web.Lists["Documents"];                 //Add together the file                 spider web.Files.Add(DocLib.RootFolder + "/TestDoc.dr.", Content, true);                   /*** If you lot want to add inside a folder: say "It" *******/                 // Become the binder called "IT"                 SPFolder SubFolder = DocLib.RootFolder.SubFolders["It"];                 //Add the file to the sub-binder                 SPFile file = SubFolder.Files.Add(SubFolder.Url + "/NewFile.doc", Content, true);                 SubFolder.Update();                   //IF yous want to Update the Meta data column say "Country"                 SPListItem item = DocLib.Items[file.UniqueId];                 item["State"] = "India";                 item.Update();                 // OR We can use the Hash Table                 var Metadata = new Hashtable { { "State", "India" } };                 // Add together the file                 web.Files.Add(DocLib.RootFolder + "/TestDoc2.doc", Content, Metadata, truthful);                                    if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)                 {                     file.CheckIn("File uploaded Successfully !");                 }                                    //file.Publish("File published Successfully !");                 }             }         }     } }                      
  • If you are looking for a way to motility files between two libraries in SharePoint using Powershell, you tin notice my some other article: Move files in SharePoint using PowerShell. You tin detect detailed lawmaking here.

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

haszleruporequity.blogspot.com

Source: https://www.sharepointgems.com/2020/03/programmatically-upload-a-file-in-sharepoint-document-library/