2014-10-12

PowerShell function to Copy the properties of Document Set, including Welcome Page and Allowed Content Types

While ostensibly simple the process to copy allowed content types and welcome page properties took a while to figure out.
The function takes 2 input; the source Content Type (CT) for the Document Set (DS), and the target CT for the DS. It assumes both have been created and referenced.
It then gets a reference to the source DS Template, and loops through its properties to copy them to the target CT.
There are a couple of 2 second sleeps in there bcos in the environment where I was using it sometimes the re-referencing wouldn't work without them.


function Copy_DocSet_Properties([Microsoft.SharePoint.SPContentType]$oTargetDSCT, [Microsoft.SharePoint.SPContentType]$oSourceDSCT) { Write-Host "`t Setting '$($oTargetDSCT.Name)' Welcome page properties" if($oSourceDSCT -ne $null) { #https://msdn.microsoft.com/en-us/library/microsoft.office.documentmanagement.documentsets(v=office.14).aspx # AllowedContentTypes Property Microsoft.Office.DocumentManagement.DocumentSets.AllowedContentTypeCollection AllowedContentTypes {get;} # DefaultDocuments Property Microsoft.Office.DocumentManagement.DocumentSets.DefaultDocumentCollection DefaultDocuments {get;} # Equals Method bool Equals(System.Object obj) # GetHashCode Method int GetHashCode() # GetType Method type GetType() # SharedFields Property Microsoft.Office.DocumentManagement.DocumentSets.SharedFieldCollection SharedFields {get;} # ToString Method string ToString() # Update Method void Update(bool bPushDown) # WelcomePageFields Property Microsoft.Office.DocumentManagement.DocumentSets.WelcomePageFieldCollection WelcomePageFields {get;} # WelcomePageView Property Microsoft.SharePoint.SPView WelcomePageView {get;set;} $oSourceDSCTT = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSetTemplate]::GetDocumentSetTemplate($oSourceDSCT) $oTargetDSCT1 = $oTargetDSCT.ParentWeb.ContentTypes[$oTargetDSCT.Id] $oNewDST = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSetTemplate]::GetDocumentSetTemplate($oTargetDSCT1) $oNewDST.AllowedContentTypes.Clear() foreach($ctId in $oSourceDSCTT.AllowedContentTypes.GetEnumerator()) { $DSACT = $oTargetDSCT.ParentWeb.ContentTypes[$ctId] if(!$DSACT) { $DSACT = Create_CTFS $oTargetDSCT.ParentWeb $($oWebSource.ContentTypes[$ctId].Name) } if($DSACT) { $oNewDST.AllowedContentTypes.Add($DSACT.Id) Write-Host "`t Added Allowed Content Type '$($DSACT.Name)'" } else { Write-Host "[ERROR] Content Type '$($oWebSource.ContentTypes[$ctId].Name) ($ctId)' doesn't exist, so it can't be added to the Document Set" "red" } } $oNewDST.Update($false) sleep 2 $oTargetDSCT1 = $oTargetDSCT.ParentWeb.ContentTypes[$oTargetDSCT.Id] $oNewDST = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSetTemplate]::GetDocumentSetTemplate($oTargetDSCT1) foreach($oSFld in $oSourceDSCTT.WelcomePageFields.GetEnumerator()) { $oField = $oTargetDSCT1.Fields[$oSFld.Title] if($oField) { $oNewDST.WelcomePageFields.Add($oField) Write-Host "`t Added Welcome Page Field '$($oField.Title)'" } else { Write-Host "[ERROR] Unable to get reference to Source Field '$($oSFld.Title)' in content type $($oTargetDSCT1.Name)" } } $oNewDST.Update($false) sleep 2 $oTargetDSCT1.Update($false) $oTargetDSCT1 = $oTargetDSCT.ParentWeb.ContentTypes[$oTargetDSCT.Id] foreach($doc in $oSourceDSCT.XmlDocuments) { [xml]$xmldoc = $doc # "http://schemas.microsoft.com/office/documentsets/sharedfields" # "http://schemas.microsoft.com/office/documentsets/defaultdocuments" # "http://schemas.microsoft.com/office/documentsets/welcomepageview" # "http://schemas.microsoft.com/sharepoint/events" # "http://schemas.microsoft.com/office/documentsets/allowedcontenttypes" # "http://schemas.microsoft.com/office/documentsets/welcomepagefields" # "http://schemas.microsoft.com/sharepoint/v3/contenttype/forms" # "http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url" if(($xmldoc.FirstChild.Attributes[0].Value -notmatch "welcomepagefields") -and ($xmldoc.FirstChild.Attributes[0].Value -notmatch "events") -and ($xmldoc.FirstChild.Attributes[0].Value -notmatch "forms")) { Write-Host "`t Updating '$($xmldoc.FirstChild.Attributes[0].Value)'" try { $oTargetDSCT1.XmlDocuments.Delete($xmldoc.FirstChild.Attributes[0].Value) #$oTargetDSCT1.Update($false) $oTargetDSCT1.XmlDocuments.Add($doc) } catch { Write-Host "[ERROR] In Copy_DocSet_Properties '$($oTargetDSCT.Name)' unable to Set Welcome Page Properties" "red" Write-Host "[ERROR] $($_.Exception)" "red" Write-Host "[ERROR] $($_.ScriptStackTrace)" "red" } } } $oTargetDSCT1.Update($false) } }

2014-09-12

Generic PowerShell Function to Create a Document Set SharePoint 2013 On-Prem

The function accepts 4 paramters the Site Url, the Library name where the Document Set will be created, the Content Type of the Document Set and finally the Name of the Document Set.
To set properties for the Document Set, and simple modification would be to include a key pair Hash table, the function declares an empty one ($dsProps) 

Example would be:
[hashtable] $dsProps = @{"Key" = "Value";"Key" = "Value";}

or
$dsProps["Key"]="Value" 
$dsProps["Key"]="Value"

The function:
function createDocumentSets([string]$SiteUrl, [string]$LibraryName, [string]$CTName, [string]$dsName) { [Microsoft.SharePoint.SPWeb]$oWeb = Get-SPWeb $SiteUrl [Microsoft.SharePoint.SPList]$oLibrary = $oWeb.Lists[$LibraryName] if($oLibrary.Title -eq $LibraryName) { [Microsoft.SharePoint.SPContentType]$oCT = $oLibrary.ContentTypes[$CTName] if($oCT.Name -eq $CTName) { [Hashtable]$dsProps = @{} [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]$newDs =
[Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($oLibrary.RootFolder,
$dsName,$oCT.ID,$dsProps,$true)
Write-Host "`tCreating '$dsName' ($CTName) in Library '$LibraryName' @ '$SiteUrl'" } else { Write-Host "[ERROR] Getting Content Type '$CTName' on Library '$LibraryName' @ '$SiteUrl'" } } else { Write-Host "[ERROR] Getting Library '$LibraryName' @ '$SiteUrl'" } $oWeb.Dispose() }