Skip to content

Instantly share code, notes, and snippets.

@svarukala
Last active March 22, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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

Here is a sample output from this script
image

@ZmanYo
Copy link

ZmanYo commented Oct 26, 2020

This doesn't work for me, the script run fine but returned doc set content 0 - looks like the contentype is not found after I put my contenttype name

@svarukala
Copy link
Author

Did you edit line 27, to replace "document set" with your own content type name? If so, undo that change and try again please.

@ZmanYo
Copy link

ZmanYo commented Oct 26, 2020

Thanks for your quick reply. I have not changed the "document set" nor the 'Computed' in line 27. Here is the output
Document Library: TestingDocSetVersion
Total Item Count: 4
Found ( 0 ) DocumentSet content type items.

@svarukala
Copy link
Author

Are you sure there are Document Sets created in that library? Create few document sets and then try to run it again. I am sure you are of these steps:
Document sets must be first activated using Site Collection features, then you enable Adv. Settings for Library, and then add Doc Set content type to the library. Only then you can create Document Sets.

@ZmanYo
Copy link

ZmanYo commented Oct 27, 2020

Yes I have activated Document Set feature on the site collection and created document set content type and I have created two document set items and uploaded attachments in their - actually the total count shows 4 items as you can see from the PS out put above. I am a SharePoint developer and I done all the document set settings rights as I have a couple of other similar libraries based on Doc set content type. I see you have a variable $CreateNewVersion = $true but not used or called anywhere in the script? or what's the PS module version that is required for this script if that's the issue?

@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