Guest User

Sharepoint download all including sub folders

a guest
Jul 18th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #Load SharePoint CSOM Assemblies
  2. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
  3. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  4.  
  5. Function Download-AllFilesFromLibrary()
  6. {
  7. param
  8. (
  9. [Parameter(Mandatory=$true)] [string] $SiteURL,
  10. [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
  11. [Parameter(Mandatory=$true)] [string] $TargetFolder
  12. )
  13. Try {
  14.  
  15. #Create Local Folder, if it doesn't exist
  16. $FolderName = ($SourceFolder.ServerRelativeURL) -replace "/","\"
  17. $LocalFolder = $TargetFolder + $FolderName
  18. If (!(Test-Path -Path $LocalFolder)) {
  19. New-Item -ItemType Directory -Path $LocalFolder | Out-Null
  20. }
  21.  
  22. #Get all Files from the folder
  23. $FilesColl = $SourceFolder.Files
  24. $Ctx.Load($FilesColl)
  25. $Ctx.ExecuteQuery()
  26.  
  27. #Iterate through each file and download
  28. Foreach($File in $FilesColl)
  29. {
  30. $TargetFile = $LocalFolder+"\"+$File.Name
  31. #Download the file
  32. $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$File.ServerRelativeURL)
  33. $WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)
  34. $FileInfo.Stream.CopyTo($WriteStream)
  35. $WriteStream.Close()
  36. write-host -f Green "Downloaded File:"$TargetFile
  37. }
  38.  
  39. #Process Sub Folders
  40. $SubFolders = $SourceFolder.Folders
  41. $Ctx.Load($SubFolders)
  42. $Ctx.ExecuteQuery()
  43. Foreach($Folder in $SubFolders)
  44. {
  45. If($Folder.Name -ne "Forms")
  46. {
  47. #Call the function recursively
  48. Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $Folder -TargetFolder $TargetFolder
  49. }
  50. }
  51. }
  52. Catch {
  53. write-host -f Red "Error Downloading Files from Library!" $_.Exception.Message
  54. }
  55. }
  56.  
  57. #Set parameter values
  58. $SiteURL="https://crescent.sharepoint.com/sites/sales/"
  59. $LibraryName="Documents"
  60. $TargetFolder="C:\Docs"
  61.  
  62. #Setup Credentials to connect
  63. $Cred= Get-Credential
  64. $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  65.  
  66. #Setup the context
  67. $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
  68. $Ctx.Credentials = $Credentials
  69.  
  70. #Get the Library
  71. $List = $Ctx.Web.Lists.GetByTitle($LibraryName)
  72. $Ctx.Load($List)
  73. $Ctx.Load($List.RootFolder)
  74. $Ctx.ExecuteQuery()
  75.  
  76.  
  77. #Call the function to download file
  78. Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $List.RootFolder -TargetFolder $TargetFolder
  79.  
  80.  
  81. #Read more: http://www.sharepointdiary.com/2017/03/sharepoint-online-download-all-files-from-document-library-using-powershell.html#ixzz5u3iP1Hx1
Add Comment
Please, Sign In to add comment