SharePoint 2016/2013/Online: How to Upload Files to SharePoint Document Libraries by PowerShell Automation

File upload operations are quite frequently used and often tricky when the SharePoint governance allows only SharePoint Client Side Technologies.

Recently I have come across a requirement in one of my assignments where an automated process is required to Sync the network shares with SharePoint.

This provides me the opportunity to write down the automation process using PowerShell +CSOM combination.

In this demo we will explore the PowerShell code to upload the file to SharePoint Online/On Premise Implementations which represents just a part of the complete automation process.

In order to setup this demo I have created as document library in SharePoint Online Site as shown below:

123

Let’s consider that we have a File Share somewhere on the network having files to be uploaded to the SharePoint Document Library

4

Now let’s explore the code step by step:

Step 1: In this step Client connection has been setup and the Client Context has been initiated.

The below is representing the connection to SharePoint Online Site but this will be little different in case if you want to initiate the connection with SharePoint On Premise site there will be a slight difference in the code.

For details you can visit to my earlier blogs at below URLs:

https://howtodowithsharepoint.wordpress.com/category/sharepoint-2013/sharepoint-online/

https://howtodowithsharepoint.wordpress.com/category/powershell-with-csom/

5

Step 2: In this step we will instantiate the Document Library Object that will provide us the handle on the desired document library in SharePoint Online Site.

This handle will let us allow getting reference of the desired folder in the document library. In this demo we are referring the Root Folder of the document library

Step 3: In this step we will explore the File System folder by using Get-ChildItem cmdlet on the using the specific Folder path

Step 4: In this step we will call another supporting function to upload the files to SharePoint Document Library

6

Inside UploadFile function-

Step 5: In this step File Object has been initialized using “System.IO” namespace. The file object will provide us the handle on files in the disk folder, open the file and read the content of the file in form of File Stream

Step 6: In this step we will add the file to the SharePoint Document Library folder (Root Folder in this case) as File Stream that we get it in Step 5

Step 7: Load File object & execute query to get it file actually uploaded to SharePoint Document Library

Step 8: Check in the file that has been uploaded in the Step 7

Step 9: This is showing the function call to “UploadFileToLibrary” function

7

That is all for the code.

In order to test this script we can either choose SharePoint (2016/2013/Online) Management Shell or Windows PowerShell.

Since in this demo I demonstrating the PowerShell in Conjunction with CSOM that I am going to run on client machines where we don’t have SharePoint Management Shell installed so I am going to use Windows PowerShell only.

Search for Windows PowerShell and launch the Windows PowerShell Command Prompt as shown below:

8

Refer the script & execute it

9

Provide password to when asked for

10

Once the PowerShell Script executes successfully, we can see the success messages printed on the Command Prompt

11

After the successful execution of the PowerShell script we can go back to the SharePoint Online Site and navigate to the document library that is the destination for these newly uploaded documents and sure enough we will get the documents

12

In case if you want to achieve additional automation scenarios you can execute this script as part of the Scheduled Task and that will further reduce human errors by executing the process automatically for you.

That is all for this demo.

Hope you find it helpful.

Advertisements

2 thoughts on “SharePoint 2016/2013/Online: How to Upload Files to SharePoint Document Libraries by PowerShell Automation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s