ArcherJayden

CSOM

May 27th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. # Load the SharePoint 2013 .NET Framework Client Object Model libraries. #
  2. [void][Reflection.Assembly]::LoadFrom("c:\Microsoft.SharePoint.Client.dll")
  3. [void][Reflection.Assembly]::LoadFrom("c:\Microsoft.SharePoint.Client.Runtime.dll")
  4. Clear-Host
  5.  
  6. $serverURL = "https://contoso.sharepoint.com/sites/test"
  7. $siteUrl = $serverURL+"/documents”
  8. $destination = "C:\temp\"
  9. $DocumentLibary = "/Shared Documents"
  10. $downloadEnabled = $true
  11. $versionEnabled = $false
  12.  
  13. #Setup Authentication Manager
  14. $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
  15. $Ctx = $AuthenticationManager.GetWebLoginClientContext($serverURL)
  16. $Ctx.Load($Ctx.Web)
  17. $Ctx.ExecuteQuery()
  18.  
  19. Write-Host $Ctx.Web.Title
  20.  
  21. function HTTPDownloadFile($ServerFileLocation, $DownloadPath)
  22. {
  23. #Download the file from the version's URL, download to the $DownloadPath location
  24. $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
  25. $Ctx = $AuthenticationManager.GetWebLoginClientContext($serverURL)
  26. $Ctx.Load($Ctx.Web)
  27. $Ctx.ExecuteQuery()
  28.  
  29. Write-Host $Ctx.Web.Title
  30. Write-Output "Download From ->'$ServerFileLocation'"
  31. Write-Output "Write to->'$DownloadPath'"
  32.  
  33. $webclient.DownloadFile($ServerFileLocation,$DownloadPath)
  34. }
  35.  
  36. function DownloadFile($theFile, $DownloadPath)
  37. {
  38. $fileRef = $theFile.ServerRelativeUrl;
  39. Write-Host $fileRef;
  40. $fileInfo = [Microsoft.sharepoint.client.File]::OpenBinaryDirect($clientContext, $fileRef);
  41. $fileStream = [System.IO.File]::Create($DownloadPath)
  42. $fileInfo.Stream.CopyTo($fileStream);
  43. $fileStream.Close()
  44. }
  45.  
  46. function Get-FileVersions ($file, $destinationFolder)
  47. {
  48. $clientContext.Load($file.Versions)
  49. $clientContext.ExecuteQuery()
  50. foreach($version in $file.Versions)
  51. {
  52. #Add version label to file in format: [Filename]_v[version#].[extension]
  53. $filesplit = $file.Name.split(".")
  54. $fullname = $filesplit[0]
  55. $fileext = $filesplit[1]
  56. $FullFileName = $fullname+"_v"+$version.VersionLabel+"."+$fileext
  57.  
  58. #Can't create an SPFile object from historical versions, but CAN download via HTTP
  59. #Create the full File URL using the Website URL and version's URL
  60. $ServerFileLocation = $siteUrl+"/"+$version.Url
  61.  
  62. #Full Download path including filename
  63. $DownloadPath = $destinationfolder+"\"+$FullFileName
  64.  
  65. if($downloadenabled) {HTTPDownloadFile "$ServerFileLocation" "$DownloadPath"}
  66.  
  67. }
  68. }
  69.  
  70. function Get-FolderFiles ($Folder)
  71. {
  72. $clientContext.Load($Folder.Files)
  73. $clientContext.ExecuteQuery()
  74.  
  75. foreach ($file in $Folder.Files)
  76. {
  77.  
  78. $folderName = $Folder.ServerRelativeURL
  79. $folderName = $folderName -replace "/","\"
  80. $folderName = $destination + $folderName
  81. $fileName = $file.name
  82. $fileURL = $file.ServerRelativeUrl
  83.  
  84.  
  85. if (!(Test-Path -path $folderName))
  86. {
  87. $dest = New-Item $folderName -type directory
  88. }
  89.  
  90. Write-Output "Destination -> '$folderName'\'$filename'"
  91.  
  92. #Create the full File URL using the Website URL and version's URL
  93. $ServerFileLocation = $serverUrl+$file.ServerRelativeUrl
  94.  
  95. #Full Download path including filename
  96. $DownloadPath = $folderName + "\" + $file.Name
  97.  
  98. #if($downloadEnabled) {HTTPDownloadFile "$ServerFileLocation" "$DownloadPath"}
  99. if($downloadEnabled) {DownloadFile $file "$DownloadPath"}
  100.  
  101. if($versionEnabled) {Get-FileVersions $file $folderName}
  102.  
  103. }
  104. }
  105.  
  106.  
  107. function Recurse($Folder)
  108. {
  109.  
  110. $folderName = $Folder.Name
  111. $folderItemCount = $folder.ItemCount
  112.  
  113. Write-Output "List Name ->'$folderName'"
  114. Write-Output "Number of List Items->'$folderItemCount'"
  115.  
  116. if($Folder.name -ne "Forms")
  117. {
  118. #Write-Host $Folder.Name
  119. Get-FolderFiles $Folder
  120. }
  121.  
  122. Write-Output $folder.ServerRelativeUrl
  123.  
  124. $thisFolder = $clientContext.Web.GetFolderByServerRelativeUrl($folder.ServerRelativeUrl)
  125. $clientContext.Load($thisFolder)
  126. $clientContext.Load($thisFolder.Folders)
  127. $clientContext.ExecuteQuery()
  128.  
  129. foreach($subfolder in $thisFolder.Folders)
  130. {
  131. Recurse $subfolder
  132. }
  133. }
  134.  
  135.  
  136. function Parse-Lists ($Lists)
  137. {
  138. $clientContext.Load($Lists)
  139. $clientContext.Load($Lists.RootFolder.Folders)
  140. $Ctx.ExecuteQuery()
  141.  
  142. foreach ($Folder in $Lists.RootFolder.Folders)
  143. {
  144. if ($Folder.name -eq "sample"){ #onlydownload selected folder
  145. recurse $Folder
  146. }
  147. }
  148.  
  149. }
  150.  
  151. $rootWeb = $clientContext.Web
  152. $LibLists = $rootWeb.lists.getByTitle($DocumentLibary)
  153. $clientContext.Load($rootWeb)
  154. $clientContext.load($LibLists)
  155. $Ctx.ExecuteQuery()
  156.  
  157. Parse-Lists $LibLists
Add Comment
Please, Sign In to add comment