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 we 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 with 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-Premises Implementations which represents just a part of the complete automation process.
In order to set up this demo we have created a document library in SharePoint Online Site as shown below:
https://howtodowithsharepoint.files.wordpress.com/2016/10/1.png?w=800https://howtodowithsharepoint.files.wordpress.com/2016/10/2.png?w=800https://howtodowithsharepoint.files.wordpress.com/2016/10/3.png?w=800
Let’s consider that we have a File Share somewhere on the network having files to be uploaded to the SharePoint Document Library
https://howtodowithsharepoint.files.wordpress.com/2016/10/4.png?w=800
Now let’s explore the code step by step:
Step 1: In this step Client connection has been set up 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-Premises site there will be a slight difference in the code.
For details you can visit my earlier blogs at below URLs:
https://howtodowithsharepoint.wordpress.com/category/sharepoint-2013/sharepoint-online/
https://howtodowithsharepoint.wordpress.com/category/powershell-with-csom/
https://howtodowithsharepoint.files.wordpress.com/2016/10/5.png?w=800
Step 2: In this step, we will instantiate the Document Library Object that will provide us with the handle on the desired document library in SharePoint Online Site.
This handle will let us allow getting the reference to 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
https://howtodowithsharepoint.files.wordpress.com/2016/10/6.png?w=800
Inside UploadFile function-
Step 5: In this step File Object has been initialized using “System.IO” namespace. The file object will provide us with 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 and execute a query to get it file actually uploaded to SharePoint Document Library
Step 8: Check in the file that has been uploaded in Step 7
Step 9: This is showing the function call to “UploadFileToLibrary” function
https://howtodowithsharepoint.files.wordpress.com/2016/10/7.png?w=800
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 we demonstrating the PowerShell in Conjunction with CSOM that we're going to run on client machines where we don’t have SharePoint Management Shell installed so we're going to use Windows PowerShell only.
Search for Windows PowerShell and launch the Windows PowerShell Command Prompt as shown below:
https://howtodowithsharepoint.files.wordpress.com/2016/10/8.png?w=800
Refer the script and execute it
https://howtodowithsharepoint.files.wordpress.com/2016/10/9.png?w=800
Provide a password to when asked for
https://howtodowithsharepoint.files.wordpress.com/2016/10/10.png?w=800
Once the PowerShell Script executes successfully, we can see the success messages printed on the Command Prompt
https://howtodowithsharepoint.files.wordpress.com/2016/10/11.png?w=800
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
https://howtodowithsharepoint.files.wordpress.com/2016/10/12.png?w=800
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.