sharepointpowershell-3.0csom

downloading a file from SharePoint Online with PowerShell


I have a requirement to download files from a sharepoint online document library using powershell

I've managed to get to the point where the download should happen but no luck.

I know its something to do with how I am using the stream/writer

any hints would be greatly appreciated

*Edit No error messages are thrown just 0 length files in my local Directory

$SPClient =  [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$SPRuntime = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$webUrl =  Read-Host -Prompt "HTTPS URL for your SP Online 2013 site" 
$username = Read-Host -Prompt "Email address for logging into that site" 
$password = Read-Host -Prompt "Password for $username" -AsSecureString
$folder = "PoSHTest" 
$destination = "C:\\test"

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$web = $ctx.Web
$lists = $web.Lists.GetByTitle($folder)
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000) 
$result = $lists.GetItems($query)
$ctx.Load($Lists)
$ctx.Load($result)
$ctx.ExecuteQuery()

#Edited the foreach as per @JNK
foreach ($File in $result) {
         Write-host "Url: $($File["FileRef"]), title: $($File["FileLeafRef"]) "
        $binary = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$File["FileRef"])
        $Action = [System.IO.FileMode]::Create 
        $new = $destination + "\\" + $File["FileLeafRef"]
        $stream = New-Object System.IO.FileStream $new, $Action 
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()

}


Solution

  • While the CSOM code above likely can be made to work I find it easier to use the web client method.

    (from http://soerennielsen.wordpress.com/2013/08/25/use-csom-from-powershell/)

    I've used the code below, to retrieve a bunch of files (metadata from CSOM queries) to a folder (using your $result collection, other params should be adjusted a bit):

    #$siteUrlString site collection url
    #$outPath path to export directory
    
    
    $siteUri = [Uri]$siteUrlString
    $client = new-object System.Net.WebClient
    $client.UseDefaultCredentials=$true
    
    if ( -not (Test-Path $outPath) ) {
        New-Item $outPath -Type Directory  | Out-Null
    }
    
    $result |% {
        $url = new-object Uri($siteUri, $_["FileRef"])
        $fileName = $_["FileLeafRef"]
        $outFile = Join-Path $outPath $fileName
        Write-Host "Downloading $url to $outFile"
    
        try{
            $client.DownloadFile( $url, $outFile )      
        }
        catch{
            #one simple retry...
            try{
                $client.DownloadFile( $url, $outFile )      
            }
            catch{
                write-error "Failed to download $url, $_"
            }
        }
    }   
    

    The trick here is the $client.UseDefaultCredentials=$true

    which will authenticate the webclient for you (as the current user).