Download all images from all picture libraries on Sharepoint Online

…this post was published on 2017 January 19.

You can use following script. Save it  on your PC or run it directly using .ISE.
All Images will be saved in specific picture library​.

You need to download locally 2 libraries , if on your machine you don’t have SP on-premises installed. These 2 libraries are
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
To run the script, you need to know the site’s URL and credentials to access the site.

<#
DESCRIPTION
This script Will permit to downlaod locally all pictures from al Picture libraries in Sharepoint Online site
You need to know 1 mandatory parameters:
    – URL         : the URL for the site
Based on thus URL, it will create in current directory a new directoryy, and there for each Picture library a sub-folder with pictures
CopyRight
This script can be use as it is or can be changed based on your needs. For more details please feel free and contact me
Romeo Donca, January 2017
romeodonca@outlook.com
http://www.romeodonca.ro
#>
 
param(
[Parameter(Mandatory=$true,HelpMessage=”The url for the tenant where the picture library is, like https://TENANT-public.sharepoint.com”)][string]$Url
) 
cls
Add-Type -Path “local-path\Microsoft.SharePoint.Client.dll”
Add-Type -Path “local-path\Microsoft.SharePoint.Client.Runtime.dll”
Import-Module MSOnline
cls
$global:cred = Get-Credential
Connect-MsolService -Credential $global:cred -ErrorAction SilentlyContinue
#$url = “https://romeodonca-public.sharepoint.com/lme”
if (!(Test-Path ($url.Replace(‘:’,”)).replace(‘/’,”)))
    {
    New-Item -ItemType directory -name ($url.Replace(‘:’,”)).replace(‘/’,”)
    }
    Set-Location  ($url.Replace(‘:’,”)).replace(‘/’,”)
    $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    $cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.username, $cred.Password) 
    $clientContext.Credentials = $cred
    $web = $clientContext.Web
    $clientContext.Load($web)
    $clientContext.ExecuteQuery()
    $lists = $web.Lists
    $clientContext.Load($lists)
    $clientContext.ExecuteQuery()
foreach ($list  in $lists)
{
 $library=$list.Title
    if ($list.BaseTemplate -eq 109)
    {
        “working on “+ $library
        $path0 = (($url.Replace(‘:’,”)).replace(‘/’,”))+’/’+$library
        if (!(Test-Path $library))
        {
            new-item -ItemType Directory $library
        }
        $clientContext.Load($lists)
        $clientContext.ExecuteQuery()
        $mylist = $web.GetList($url+”/”+$library)
        $clientContext.Load($mylist)
        $clientContext.ExecuteQuery()
        $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $items = $mylist.GetItems($query)
        $clientContext.Load($items)
        $clientContext.ExecuteQuery()
        foreach ($item in $items)
        {
            Invoke-WebRequest -Uri (‘http://www.romeodonca.ro’+$item.FieldValues[‘FileRef’]) -OutFile (Join-Path $library  $item.FieldValues[‘FileLeafRef’])
        }
    }
}
This entry was posted in Powershell, Sharepoint Online. Bookmark the permalink.