Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Load the SharePoint 2013 .NET Framework Client Object Model libraries. #
- [void][Reflection.Assembly]::LoadFrom("c:\Microsoft.SharePoint.Client.dll")
- [void][Reflection.Assembly]::LoadFrom("c:\Microsoft.SharePoint.Client.Runtime.dll")
- Clear-Host
- $serverURL = "https://contoso.sharepoint.com/sites/test"
- $siteUrl = $serverURL+"/documents”
- $destination = "C:\temp\"
- $DocumentLibary = "/Shared Documents"
- $downloadEnabled = $true
- $versionEnabled = $false
- #Setup Authentication Manager
- $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
- $Ctx = $AuthenticationManager.GetWebLoginClientContext($serverURL)
- $Ctx.Load($Ctx.Web)
- $Ctx.ExecuteQuery()
- Write-Host $Ctx.Web.Title
- function HTTPDownloadFile($ServerFileLocation, $DownloadPath)
- {
- #Download the file from the version's URL, download to the $DownloadPath location
- $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
- $Ctx = $AuthenticationManager.GetWebLoginClientContext($serverURL)
- $Ctx.Load($Ctx.Web)
- $Ctx.ExecuteQuery()
- Write-Host $Ctx.Web.Title
- Write-Output "Download From ->'$ServerFileLocation'"
- Write-Output "Write to->'$DownloadPath'"
- $webclient.DownloadFile($ServerFileLocation,$DownloadPath)
- }
- function DownloadFile($theFile, $DownloadPath)
- {
- $fileRef = $theFile.ServerRelativeUrl;
- Write-Host $fileRef;
- $fileInfo = [Microsoft.sharepoint.client.File]::OpenBinaryDirect($clientContext, $fileRef);
- $fileStream = [System.IO.File]::Create($DownloadPath)
- $fileInfo.Stream.CopyTo($fileStream);
- $fileStream.Close()
- }
- function Get-FileVersions ($file, $destinationFolder)
- {
- $clientContext.Load($file.Versions)
- $clientContext.ExecuteQuery()
- foreach($version in $file.Versions)
- {
- #Add version label to file in format: [Filename]_v[version#].[extension]
- $filesplit = $file.Name.split(".")
- $fullname = $filesplit[0]
- $fileext = $filesplit[1]
- $FullFileName = $fullname+"_v"+$version.VersionLabel+"."+$fileext
- #Can't create an SPFile object from historical versions, but CAN download via HTTP
- #Create the full File URL using the Website URL and version's URL
- $ServerFileLocation = $siteUrl+"/"+$version.Url
- #Full Download path including filename
- $DownloadPath = $destinationfolder+"\"+$FullFileName
- if($downloadenabled) {HTTPDownloadFile "$ServerFileLocation" "$DownloadPath"}
- }
- }
- function Get-FolderFiles ($Folder)
- {
- $clientContext.Load($Folder.Files)
- $clientContext.ExecuteQuery()
- foreach ($file in $Folder.Files)
- {
- $folderName = $Folder.ServerRelativeURL
- $folderName = $folderName -replace "/","\"
- $folderName = $destination + $folderName
- $fileName = $file.name
- $fileURL = $file.ServerRelativeUrl
- if (!(Test-Path -path $folderName))
- {
- $dest = New-Item $folderName -type directory
- }
- Write-Output "Destination -> '$folderName'\'$filename'"
- #Create the full File URL using the Website URL and version's URL
- $ServerFileLocation = $serverUrl+$file.ServerRelativeUrl
- #Full Download path including filename
- $DownloadPath = $folderName + "\" + $file.Name
- #if($downloadEnabled) {HTTPDownloadFile "$ServerFileLocation" "$DownloadPath"}
- if($downloadEnabled) {DownloadFile $file "$DownloadPath"}
- if($versionEnabled) {Get-FileVersions $file $folderName}
- }
- }
- function Recurse($Folder)
- {
- $folderName = $Folder.Name
- $folderItemCount = $folder.ItemCount
- Write-Output "List Name ->'$folderName'"
- Write-Output "Number of List Items->'$folderItemCount'"
- if($Folder.name -ne "Forms")
- {
- #Write-Host $Folder.Name
- Get-FolderFiles $Folder
- }
- Write-Output $folder.ServerRelativeUrl
- $thisFolder = $clientContext.Web.GetFolderByServerRelativeUrl($folder.ServerRelativeUrl)
- $clientContext.Load($thisFolder)
- $clientContext.Load($thisFolder.Folders)
- $clientContext.ExecuteQuery()
- foreach($subfolder in $thisFolder.Folders)
- {
- Recurse $subfolder
- }
- }
- function Parse-Lists ($Lists)
- {
- $clientContext.Load($Lists)
- $clientContext.Load($Lists.RootFolder.Folders)
- $Ctx.ExecuteQuery()
- foreach ($Folder in $Lists.RootFolder.Folders)
- {
- if ($Folder.name -eq "sample"){ #onlydownload selected folder
- recurse $Folder
- }
- }
- }
- $rootWeb = $clientContext.Web
- $LibLists = $rootWeb.lists.getByTitle($DocumentLibary)
- $clientContext.Load($rootWeb)
- $clientContext.load($LibLists)
- $Ctx.ExecuteQuery()
- Parse-Lists $LibLists
Add Comment
Please, Sign In to add comment