Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import-module microsoft.online.sharepoint.powershell
  2.  
  3. $siteUrl = "https://company-my.sharepoint.com/personal/user_company_com"
  4. $username ="adminuser@company.com"
  5. $password = Read-Host -Prompt "Enter password" -AsSecureString
  6. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
  7.  
  8. # Create a client context connection
  9. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
  10. $ctx.Credentials = $credentials
  11.  
  12. $list = $ctx.Web.Lists.GetByTitle('Documents')
  13. $destinationPath = "C:\test"
  14.  
  15. # Connect to the list and query for all items
  16. $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
  17. $camlQuery.ViewXml = '<View><Query><Where></Where></Query></View>'
  18. $items = $list.GetItems($camlQuery)
  19. $ctx.Load($items)
  20. $ctx.ExecuteQuery()
  21. write-host "Found " $items.Count " items"
  22.  
  23. # Assuming we want to download all documents
  24. foreach ($item in $items)
  25. {
  26. Write-Host "Saving " $item["FileRef"]
  27. $fileRef = $item["FileRef"]
  28. $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$fileRef.ToString())
  29. $new = $destinationPath + "\" + $item["FileLeafRef"]
  30. [byte]$byte = ""
  31.  
  32. $list = New-Object System.Collections.Generic.List[byte]
  33. #Read in each file
  34. try {
  35. while(($byte = $fileInfo.Stream.ReadByte()) -ne -1)
  36. {
  37. $list.Add($byte)
  38. }
  39. } catch [Exception] {
  40. #return $_.Exception.Message
  41. }
  42.  
  43. [System.IO.File]::WriteAllBytes($new, $list.ToArray());
  44. $fileInfo.Dispose()
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement