-
-
Save svarukala/7e453f8bf0868d8b90b11f3612df83bc to your computer and use it in GitHub Desktop.
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() |
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
Did you edit line 27, to replace "document set" with your own content type name? If so, undo that change and try again please.
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.
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.
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?
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
Could you please provide JSOM version too?
Here is a sample output from this script