Creating Document Sets and its properties with Powershell in SharePoint

I am working an a project, where we want to use a Powershell Script to initialize a SharePoint workspace. This workspace should base / contain the following things:

The Powershell Script should be used for the testing system as well as for the production, so that the initialization is exactly the same.

In this post i’d like to share how it works with Site Columns and a content type for a document set. And in the document set, should also be Shared Fields and the Welcome Page Fields defined.

Here is the whole script:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

## *************** CREATE FIELDS & CONTENT TYPES ****************
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.DocumentManagement”)

function createField($fieldtype, $fieldName, $fieldDisplayName, $fieldID, $fieldDesc, $optionalParameters, $choices)
$fieldXMLString = ‘ ‘+$choices+’ ‘

#See field XML on console
#write-host $fieldXMLString

#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXMLString)

write-host “field was created successfully”
>

$web = get-spweb $site

# FIELDS
createField -fieldtype “Text” -fieldName “AccountID” -fieldDisplayName “Account ID” -fieldDesc “Account ID ”
createField -fieldtype “Text” -fieldName “AccountName” -fieldDisplayName “Account Name” -fieldDesc “Account Name ”
createField -fieldtype “Text” -fieldName “AccountNumber” -fieldDisplayName “Account Number” -fieldDesc “Account Number”
createField -fieldtype “Text” -fieldName “BusinessNumber” -fieldDisplayName “Business Number” -fieldDesc “Business Number ”
createField -fieldtype “Text” -fieldName “ERPNumber” -fieldDisplayName “ERP Number” -fieldDesc “ERP Number ”
createField -fieldtype “Text” -fieldName “Industry” -fieldDisplayName “Industry” -fieldDesc “Industry ”
createField -fieldtype “Text” -fieldName “KAM” -fieldDisplayName “KAM” -fieldDesc “KAM”
createField -fieldtype “Choice” -fieldName “Security” -fieldDisplayName “Security” -fieldDesc “AccountID ” -choices “ Internal Internal Public Confidential ”
#createField -fieldtype “Text” -fieldName “AccountID” -fieldDisplayName “AccountID” -fieldDesc “AccountID ”

# CONTENT TYPES
$ctName = “DocSetCustom”

#$web.AvailableContentTypes | Select-Object Name, ID
$ctParent = $web.AvailableContentTypes | Where

$ctDocumentSet = New-Object Microsoft.SharePoint.SPContentType($ctParent, $web.ContentTypes, $ctName);
$ctDocumentSet.Group =”MyGroup”;
$ctDocSetCreated = $web.ContentTypes.Add($ctDocumentSet);

$fieldLinkAccountID = $web.Fields.GetField(“AccountID”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkAccountID);
$fieldLinkAccountName = $web.Fields.GetField(“AccountName”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkAccountName);
$fieldLinkAccountNumber = $web.Fields.GetField(“AccountNumber”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkAccountNumber);
$fieldLinkBusinessNumber = $web.Fields.GetField(“BusinessNumber”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkBusinessNumber);
$fieldLinkERPNumber = $web.Fields.GetField(“ERPNumber”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkERPNumber);
$fieldLinkIndustry = $web.Fields.GetField(“Industry”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkIndustry);
$fieldLinkKAM = $web.Fields.GetField(“KAM”);
$ctDocSetCreated.FieldLinks.Add($fieldLinkKAM);

$ctDocSetCreated.Update();
$ctDocSetCreated = $web.ContentTypes[$ctName]

#Set Shared Columns
$sharedFileXMLPrefix=’ ‘
$sharedFileXML=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXMLSuffix=’ ‘

$FinalValue=$sharedFileXMLPrefix+$sharedFileXML+ $sharedFileXMLSuffix
$oXMLDocument=New-Object System.XML.XMLDocument
$ns = New-Object Xml.XmlNamespaceManager $oXMLDocument.NameTable
$ns.AddNamespace( “NamespaceURI”, “http://schemas.microsoft.com/office/documentsets/sharedfields” )
$oXMLDocument.LoadXML($FinalValue);
$ctDocSetCreated.XmlDocuments.Delete(“http://schemas.microsoft.com/office/documentsets/sharedfields”);
$ctDocSetCreated.XmlDocuments.Add($oXMLDocument);

#Set Welcome Page Fields
$sharedFileXMLPrefix=’ ‘
$sharedFileXML=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXML+=’ ‘
$sharedFileXMLSuffix=’ ‘

$FinalValue=$sharedFileXMLPrefix+$sharedFileXML+ $sharedFileXMLSuffix
$oXMLDocument=New-Object System.XML.XMLDocument
$ns = New-Object Xml.XmlNamespaceManager $oXMLDocument.NameTable
$ns.AddNamespace( “NamespaceURI”, “http://schemas.microsoft.com/office/documentsets/welcomepagefields” )
$oXMLDocument.LoadXML($FinalValue);
$ctDocSetCreated.XmlDocuments.Delete(“http://schemas.microsoft.com/office/documentsets/welcomepagefields”);
$ctDocSetCreated.XmlDocuments.Add($oXMLDocument);

#Update Content type / $true bedeutet, dass auch runtergeerbt wird in die Listen
$ctDocSetCreated.Update($true)

Ok, well. Let’s have a look at some interesting points. At first i have a function to create the fields based on CAML. After that i can create the content type “DocSetCustom”. For that i fetch the parent content type, and add it the the web content types.

After that i read the fields i want to add to the content type and add them as FieldLinks.

The tricky part is for shared fields and welcome page fields. Therefore i work in the XML (CAML) of the SchemaXML of the created content type and add those fields.

It is working like a charm. You should try it.

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.