Skip to content

Instantly share code, notes, and snippets.

@svarukala
Last active March 22, 2021 10:38
Show Gist options
  • Save svarukala/7e453f8bf0868d8b90b11f3612df83bc to your computer and use it in GitHub Desktop.
Save svarukala/7e453f8bf0868d8b90b11f3612df83bc to your computer and use it in GitHub Desktop.
PowerShell script to capture version of Document Sets using CSOM in SharePoint Online. This script also lists out all the Document Set content type items along with the version information from a document library using the latest CSOM library released recently (version: 16.1.20317.12000 or above). Before this release, the only way to version a D…
cls
#Import-Module Microsoft.Online.SharePoint.PowerShell
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.DocumentManagement.dll"
#Setting this to True will create new version for any DocSet it identifies that has zero versions existing
$CreateNewVersion = $true
$AdminPass = "password"
$AdminPassword = ConvertTo-SecureString -string $AdminPass -AsPlainText -Force
#$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
$username="MeganB@YourTenant.OnMicrosoft.com"
$Url="https://YourTenant.sharepoint.com/sites/ModernTeamSite"
$ListTitle="Documents"
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()
$list=$ctx.Web.Lists.GetByTitle($ListTitle)
$ctx.Load($list)
$ctx.ExecuteQuery()
"Document Library: "+ $ListTitle
"Total Item Count: "+ $list.ItemCount
$spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$spqQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>Document Set</Value></Eq></Where></Query></View>";
#$spqQuery.v .ViewFields = "<FieldRef Name='FileLeafRef' /><FieldRef Name='Title' />"
#$spqQuery.ViewFieldsOnly = $true
$items = $list.GetItems($spqQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()
write-host "Found (" $items.Count ") DocumentSet content type items."
write-host ""
foreach ($splListItem in $items)
{
write-host "Id: " $splListItem.Id " ," "Title:" $splListItem["Title"] -ForegroundColor Green
$folder = $splListItem.Folder;
$ctx.Load($folder);
$ctx.ExecuteQuery()
$docSetItem = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::GetDocumentSet($ctx, $folder)
$ctx.Load($docSetItem)
$ctx.ExecuteQuery()
$docSetItemVersions = $docSetItem.VersionCollection
$ctx.Load($docSetItemVersions)
$ctx.ExecuteQuery()
write-host "Total versions: " $docSetItemVersions.Count
if($docSetItemVersions.Count -gt 0)
{
$docSetItemVersions | %{
Write-Host "Fetching the version contents..." -ForegroundColor Yellow
#$_ | Get-Member
Write-Host "Version Id: "$_.VersionLabel ", Comment: " $_.Comments ", Created: " $_.Created ", By: " $_.CreatedBy
#$versionContents = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionItem]
$versionContents = [System.Collections.Generic.List[Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionItem]]$_.GetDisplayContents();
$fieldValues = [System.Collections.Generic.List[Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionField]]$_.GetDisplayFields();
$ctx.ExecuteQuery()
#Write-Host $versionContents[0]
#$versionContents[0] | Get-Member
write-host "Found (" $versionContents.Count ") items in this doc set verion:"
$versionContents | %{ write-host "-- ItemUrl:" $_.ItemUrl ", VersionLabel:" $_.VersionLabel ", InternalId: " $_.InternalId}
#$fieldValues.Count
write-host "Field values found in this version:"
$fieldValues | %{ Write-Host "-- Title: " $_.Title ", Value: "$_.FormattedValue }
Write-Host ""
}
}
else {
$docSetItemVersions.Add($true, "v1")
$ctx.ExecuteQuery()
}
write-host ""
write-host ""
}
$ctx.Dispose()
@svarukala
Copy link
Author

Ensure you have latest SPO CSOM dlls.
Here is my PS version:
❯ $PSVersionTable

Name Value


PSVersion 5.1.19041.608
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.608
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

@sankarkumar23
Copy link

Could you please provide JSOM version too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment