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:
Let’s consider that we have a File Share somewhere on the network having files to be uploaded to the SharePoint Document Library
Now let’s explore the code step by step:
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 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,
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 to the Root Folder of the document library
In this step we will explore the File System folder by using Get-ChildItem cmdlet on the using the specific Folder path
In this step we will call another supporting function to upload the files to SharePoint Document Library
Inside UploadFile function-
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
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 in Step 5
Load File object & execute query to get file actually uploaded to SharePoint Document Library
Check in the file that has been uploaded in Step 7
This is showing the function call to “UploadFileToLibrary” function
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 am 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:
Refer to the script & execute it
Provide password when asked
Once the PowerShell Script executes successfully, we can see the success messages printed on the Command Prompt
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
In case 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.