Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. using ( var ctx = new ClientContext( "siteUrl" ) )
  2. {
  3. ctx.Credentials = new SharePointOnlineCredentials( userName, secureStringPassword );
  4. var query = new CamlQuery
  5. {
  6. ViewXml = "<View Scope='RecursiveAll'>" +
  7. "<Query>" +
  8. "<Where>" +
  9. "<Eq>" +
  10. "<FieldRef Name='FSObjType'/>" +
  11. "<Value Type='Int'>0</Value>" +
  12. "</Eq>" +
  13. "</Where>" +
  14. "</Query>" +
  15. "</View>"
  16. };
  17. var web = ctx.Web;
  18. var list = web.Lists.GetByTitle( library);
  19. var items = list.GetItems( query );
  20. ctx.Load( items, collection => collection.Include( i => i.File.ServerRelativeUrl, i => i.File.Name ) );
  21. ctx.ExecuteQuery();
  22. if ( items != null )
  23. foreach(var item in items)
  24. {
  25. using ( var fi = File.OpenBinaryDirect( ctx, item.File.ServerRelativeUrl ) )
  26. {
  27. FileStream fileStream;
  28. using ( fileStream = new FileStream( @"fileDestination", FileMode.Create ) )
  29. {
  30. if ( fi != null ) fi.Stream.CopyTo( fileStream );
  31. }
  32. }
  33. }
  34. }
  35.  
  36. Add-Type –Path "C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  37. Add-Type –Path "C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  38.  
  39.  
  40.  
  41. Function Download-File(){
  42. param(
  43. $listItem = $(throw “Please provide a List Item”),
  44. $downloadPath = $(throw “Please specify a Path”)
  45. )
  46.  
  47. $context = $listItem.Context
  48. $fileUrl = $listItem["FileRef"]
  49. $fi = new-object System.IO.FileInfo($listItem["FileRef"])
  50. $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($context, $fileUrl);
  51. try{
  52. $fileName = [System.IO.Path]::Combine($downloadPath,$fi.Name)
  53. $fileStream = [System.IO.File]::Create($fileName)
  54. $fileInfo.Stream.CopyTo($fileStream)
  55. }
  56. finally {
  57. $fileStream.Dispose()
  58. }
  59. }
  60.  
  61.  
  62.  
  63. $Url = "https://tenant.sharepoint.com/"
  64. $UserName = "username@tenant.onmicrosoft.com"
  65. $Password = "password"
  66. $listTitle = "Documents"
  67. $folder = "/Shared Documents/Archive"
  68.  
  69.  
  70.  
  71. if([string]::IsNullOrEmpty($Password)) {
  72. $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
  73. }
  74. else {
  75. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
  76. }
  77.  
  78.  
  79. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  80. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
  81. $ctx.credentials = $credentials
  82.  
  83. #Retive items from a specific folder
  84. $list = $ctx.Web.Lists.GetByTitle($listTitle)
  85. $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
  86. $query.FolderServerRelativeUrl=$folder;
  87. $items = $list.GetItems($query)
  88. $ctx.Load($items)
  89. $ctx.ExecuteQuery()
  90.  
  91.  
  92. #Download file(s)
  93. foreach ($item in $items){
  94.  
  95. if($item.FileSystemObjectType -ne [Microsoft.SharePoint.Client.FileSystemObjectType ]::File) {
  96. continue
  97. }
  98. Download-File $item "C:TempArchive"
  99.  
  100.  
  101. #Delete Item
  102. $item.DeleteObject()
  103. $ctx.ExecuteQuery()
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement