Nov 21 2023 06:34 AM
I am cloning a document library containg a DocumentSet.
#Parameters
$SiteURL = "https://company.sharepoint.com/sites/Contracts"
$SourceLibraryName = "ABC"
$DestinationLibraryName = "XYZ"
Try {
#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Interactive
#Get the Source library
$SourceLibrary = Get-PnPList -Identity $SourceLibraryName -Includes RootFolder
#Get Timestamp
$Timestamp = (Get-Date).tostring("yyyyMMdd-HHmmss")
$TemplateName = $SourceLibrary.Title+"_"+$Timestamp
#Step 1: Save the Source Library as a template
$SourceLibrary.SaveAsTemplate($TemplateName, $TemplateName, [string]::Empty, $False);
Invoke-PnPQuery
#Get the Library Template created
$Ctx = Get-PnPContext
$Web = Get-PnPWeb
$RootWeb = $Ctx.Site.RootWeb
$ListTemplates = $Ctx.Site.GetCustomListTemplates($RootWeb)
$Ctx.Load($RootWeb)
$Ctx.Load($ListTemplates)
Invoke-PnPQuery
$ListTemplate = $ListTemplates | Where {$_.Name -eq $TemplateName}
#Step 2: Create the destination library from the source library template
If(!(Get-PnPList -Identity $DestinationLibraryName))
{
#Create the destination library
$ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreation.Title = $DestinationLibraryName
$ListCreation.ListTemplate = $ListTemplate
$DestinationList = $Web.Lists.Add($ListCreation)
Invoke-PnPQuery
Write-host "New Library '$DestinationLibraryName' created successfully!" -f Green
}
Else
{
Write-host "Library '$DestinationLibraryName' already exists!" -f Yellow
}
#Remove the List Template
$TemplatePath = $Web.ServerRelativeUrl+"/_catalogs/lt/"+$ListTemplate.InternalName
$TemplateFile = Get-PnPFile -Url $TemplatePath
$TemplateFile.DeleteObject()
Invoke-PnPQuery
#Remove-PnPFile -ServerRelativeUrl $TemplatePath -Recycle -Force
#Step 3: Copy content from Source to the destination library
$DestinationLibrary = Get-PnPList $DestinationLibraryName -Includes RootFolder
#Calculate Site Relative URL of the Folder
If($Web.ServerRelativeURL -eq "/")
{
$FolderSiteRelativeUrl = $SourceLibrary.RootFolder.ServerRelativeUrl
}
Else
{
$FolderSiteRelativeUrl = $SourceLibrary.RootFolder.ServerRelativeUrl.Replace($Web.ServerRelativeURL,[string]::Empty)
}
#Get All Content from Source Library's Root Folder
$RootFolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl | Where {($_.Name -ne "Forms") -and (-Not($_.Name.StartsWith("_")))}
#Copy Items to the Destination
$RootFolderItems | ForEach-Object {
$DestinationURL = $DestinationLibrary.RootFolder.ServerRelativeUrl
Copy-PnPFile -SourceUrl $_.ServerRelativeUrl -TargetUrl $DestinationURL -Force -OverwriteIfAlreadyExists
Write-host "`tCopied '$($_.ServerRelativeUrl)'" -f Green
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Basically it works and the new library contains all content types including the DocumentSet.
But the created DocumentSets in the library do not work - simply the link when cklicking the DocumentSet (folder) is wrong.
Not working link in the cloned library
company.sharepoint.com/sites/Contracts/Playground%20Vertrge/Forms/Vertragsmappe/docsethomepage.aspx?ID=3&FolderCTID=0x0120D520002509A316E8F1C644984D64608C7434D2006212E82A3A4C4849B2F334504E38B459&List=4e9ca871-9ca6-47cb-9c9b-3f26b9b08850&RootFolder=%2Fsites%2FVertragsmanagement%2FPlayground%20Vertrge%2FFab4Minds%20Vertrag&Recsrc=%2Fsites%2FVertragsmanagement%2FPlayground%20Vertrge%2FFab4Minds%20Vertrag
Working link in the original library:
company.sharepoint.com/sites/Contracts/IT%20Vertrge/Forms/AllItems.aspx?id=%2Fsites%2FVertragsmanagement%2FIT%20Vertrge%2FFab4Minds%20Vertrag
When I remove the DocumentSet content type from the cloned library and re-add it it works as expected.
Any idea?
We are still on PNP version 1.12.0