Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ( var ctx = new ClientContext( "siteUrl" ) )
- {
- ctx.Credentials = new SharePointOnlineCredentials( userName, secureStringPassword );
- var query = new CamlQuery
- {
- ViewXml = "<View Scope='RecursiveAll'>" +
- "<Query>" +
- "<Where>" +
- "<Eq>" +
- "<FieldRef Name='FSObjType'/>" +
- "<Value Type='Int'>0</Value>" +
- "</Eq>" +
- "</Where>" +
- "</Query>" +
- "</View>"
- };
- var web = ctx.Web;
- var list = web.Lists.GetByTitle( library);
- var items = list.GetItems( query );
- ctx.Load( items, collection => collection.Include( i => i.File.ServerRelativeUrl, i => i.File.Name ) );
- ctx.ExecuteQuery();
- if ( items != null )
- foreach(var item in items)
- {
- using ( var fi = File.OpenBinaryDirect( ctx, item.File.ServerRelativeUrl ) )
- {
- FileStream fileStream;
- using ( fileStream = new FileStream( @"fileDestination", FileMode.Create ) )
- {
- if ( fi != null ) fi.Stream.CopyTo( fileStream );
- }
- }
- }
- }
- Add-Type –Path "C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type –Path "C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
- Function Download-File(){
- param(
- $listItem = $(throw “Please provide a List Item”),
- $downloadPath = $(throw “Please specify a Path”)
- )
- $context = $listItem.Context
- $fileUrl = $listItem["FileRef"]
- $fi = new-object System.IO.FileInfo($listItem["FileRef"])
- $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($context, $fileUrl);
- try{
- $fileName = [System.IO.Path]::Combine($downloadPath,$fi.Name)
- $fileStream = [System.IO.File]::Create($fileName)
- $fileInfo.Stream.CopyTo($fileStream)
- }
- finally {
- $fileStream.Dispose()
- }
- }
- $Url = "https://tenant.sharepoint.com/"
- $UserName = "username@tenant.onmicrosoft.com"
- $Password = "password"
- $listTitle = "Documents"
- $folder = "/Shared Documents/Archive"
- if([string]::IsNullOrEmpty($Password)) {
- $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
- }
- else {
- $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
- }
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
- $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
- $ctx.credentials = $credentials
- #Retive items from a specific folder
- $list = $ctx.Web.Lists.GetByTitle($listTitle)
- $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
- $query.FolderServerRelativeUrl=$folder;
- $items = $list.GetItems($query)
- $ctx.Load($items)
- $ctx.ExecuteQuery()
- #Download file(s)
- foreach ($item in $items){
- if($item.FileSystemObjectType -ne [Microsoft.SharePoint.Client.FileSystemObjectType ]::File) {
- continue
- }
- Download-File $item "C:TempArchive"
- #Delete Item
- $item.DeleteObject()
- $ctx.ExecuteQuery()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement