Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Download files from a Sharepoint Online Document Library

$
0
0

Hi

I need to write a powershell script that will download the contents of a SharePoint document Library to a local folder

I have pieced together the following script which created the filenames on my local drive but all with 0 bytes

 

The error I get is "Method invocation failed because [Microsoft.SharePoint.Client.ListItem] doesn't contain a method named 'OpenBinary'"

Not too sure why this is happening 

any help would be greatly appreciated

 

========================

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

$loadInfo2 = [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()

 

foreach ($File in $result) {

          Write-host "Url: $($File["FileRef"]), title: $($File["FileLeafRef"]) "

            byte[] $binary = $File.OpenBinary()

            $stream = New-Object System.IO.FileStream($destination + "\\" + $File["FileLeafRef"]), Create

            $writer = New-Object System.IO.BinaryWriter($stream)

            $writer.write($binary)

            $writer.Close()

}

 

 

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles