By adding some flavor with an automation mechanism, this technique could be very useful to the scenarios wherein we need to download documents in bulk on a schedule based job.
In order to setup this demo, I have created a document library in my SharePoint Online Site. Though this is equally applicable to On Premise implementations of SharePoint.
I have added a new document library in the SharePoint Online Site with the name “Prashant Documents” and uploaded a set of documents to it.
I have also set up a local folder on the disk drive which will act as a destination folder for the files to be downloaded.
Step 1- Initiate the Client Context
In order to get more information on Setting up Client Context and other Environmental Configurations, you may visit one of the earlier articles SharePoint Online: How to Install SharePoint Online Management Shell.
Instantiate and load SharePoint Document Library Object.
Load the Root folder in SharePoint Document Library, which is containing the set of documents to be downloaded.
Using the “OpenBinaryDirect” method that is available within the “Microsoft.SharePoint.Client.File” class, we can read the file from the SharePoint Document Library and save the File Stream to the disk location by using the “CopyTo” method which is available in the “System.IO.File” Class.
Call the “DownloadFilesFromFolder” function that will download the files to the disk location.
On execution of this function, the Windows PowerShell Credentials prompt will appear where we have to specify the password to the login account.
Once the script is executed successfully, we can see the documents downloaded to the disk location.
Hope you find this helpful.