SharePoint Online PowerShell Script to Extract All Documents and Their Versions

SharePoint

Dowload documents SharePoint Online with versions

This script will dowload documents from SharePoint Online document library with all their versions using client object model. The script uses very good powershell module from Arleta Wanat and you can download currect module here SPOMod.

•You have to put my script with SPOMod script module together in e.g. c:\temp

•Customize the variables in download.ps1 script

•Read how to use SPOMod here (In description section): SPOMod

•Open the Powershell console and run the script download.ps1


$userName = "foo@domain.com"
$password = "password"
$siteCollectionUrl = "https://yourcollection.sharepoint.com"
$webUrl = $siteCollectionUrl + "/Sites/yourweb"
$docLibraryTitle = "Documents"
$destinationLocalFolder = "c:\temp\doc"

#------------------------------------------------
Import-Module .\SPOMod20160326.psm1
#------------------------------------------------

Connect-SPOCSOM -Username $userName -Url $webUrl

#Download document function
function HTTPDownloadFile ($serverFileLocation, $downloadPath)
{
#create secure password
$sPassword = $password | ConvertTo-SecureString -AsPlainText -Force

$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $sPassword)
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")

[System.Uri]$destinationUri = [System.Uri]$serverFileLocation

Write-Host "Downloading" $serverFileLocation "as" $downloadPath

$webClient.DownloadFile($destinationUri, $DownloadPath)
$webClient.Dispose()
}

#------------------------------------------------
#Get files (not dirs)
$listItems = Get-SPOListItems -ListTitle $docLibraryTitle -IncludeAllProperties $true -Recursive | where {$_.FsObjType -eq 0}

#Iteration on all files in doc. library
foreach ($item in $listItems)
{
[string]$sourceDirectory = $item.FileDirRef
[string]$sourceFileName = $item.FileLeafRef
[String]$sourceFileUrl = $siteCollectionUrl + $item.FileRef

#create destination directory
[string]$destinationDirectory = $destinationLocalFolder + $sourceDirectory
$destinationDirectory = $destinationDirectory.Replace("/","\")
if (!(Test-Path -path $destinationDirectory))
{
New-Item -path $destinationDirectory -type directory
}

#download current version document
[string]$destinationFilePath = $destinationDirectory + "\" + $sourceFileName
HTTPDownloadFile $sourceFileUrl $destinationFilePath

#get versions of curent file
$fileVersions = Get-SPOListItemVersions -ListTitle $docLibraryTitle -ItemID $item.Id

#iteration on all file versions
foreach ($ver in $fileVersions)
{
$fileHistoryUrl = $webUrl + "/" + $ver.Url
$version = $ver.VersionLabel

if (![string]::IsNullOrEmpty($fileHistoryUrl))
{
$filesplit = $sourceFileName.split(".")
$fullname = $filesplit[0] $fileext = $filesplit[1] $fullFileName = $fullname + "_v" + $version + "." + $fileext

#download history version document
$destinationFilePath = $destinationDirectory + "\" + $fullFileName

HTTPDownloadFile $sourceFileUrl $destinationFilePath
}
}
}

4 responses on “SharePoint Online PowerShell Script to Extract All Documents and Their Versions

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *