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:

  • Separate Database for Site Collection
  • Standard Teamsite of SharePoint
  • Permission Groups
  • Site Columns
  • Content Types from basetype Document Set

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

$site = "http://server/managedpath/Sitecollection";

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

function createField($fieldtype, $fieldName, $fieldDisplayName, $fieldID, $fieldDesc, $optionalParameters, $choices)
{
$fieldXMLString = '<field Type="'+$fieldtype+'"
Name="'+$fieldName+'"
Description="'+$fieldDesc+'"
DisplayName="'+$fieldDisplayName+'"
StaticName="'+$fieldName+'"
Group="SICK CRM"
'+$optionalParameters+'
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE">'+$choices+'</field>'

#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 "<default>Internal</default><choices><choice>Internal</choice><choice>Public</choice><choice>Confidential</choice></choices>"
#createField -fieldtype "Text" -fieldName "AccountID" -fieldDisplayName "AccountID" -fieldDesc "AccountID "

# CONTENT TYPES
$ctName = "DocSetCustom"

#$web.AvailableContentTypes | Select-Object Name, ID
$ctParent = $web.AvailableContentTypes | Where {$_.Name -eq "Document Set"}

$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='<sf:sharedfields xmlns:sf="http://schemas.microsoft.com/office/documentsets/sharedfields" LastModified="10/13/2016 10:35:08">'
$sharedFileXML='<sharedfield id="'+$fieldLinkAccountID.Id+'"></sharedfield>'
$sharedFileXML+='<sharedfield id="'+$fieldLinkAccountName.Id+'"></sharedfield>'
$sharedFileXML+='<sharedfield id="'+$fieldLinkAccountNumber.Id+'"></sharedfield>'
$sharedFileXML+='<sharedfield id="'+$fieldLinkBusinessNumber.Id+'"></sharedfield>'
$sharedFileXML+='<sharedfield id="'+$fieldLinkERPNumber.Id+'"></sharedfield>'
$sharedFileXML+='<sharedfield id="'+$fieldLinkIndustry.Id+'"></sharedfield>'
$sharedFileXML+='<sharedfield id="'+$fieldLinkKAM.Id+'"></sharedfield>'
$sharedFileXMLSuffix='</sf:sharedfields>'

$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='<wpf:welcomepagefields xmlns:wpf="http://schemas.microsoft.com/office/documentsets/welcomepagefields" LastModified="10/13/2016 10:35:08">'
$sharedFileXML='<welcomepagefield id="'+$fieldLinkAccountID.Id+'"></welcomepagefield>'
$sharedFileXML+='<welcomepagefield id="'+$fieldLinkAccountName.Id+'"></welcomepagefield>'
$sharedFileXML+='<welcomepagefield id="'+$fieldLinkAccountNumber.Id+'"></welcomepagefield>'
$sharedFileXML+='<welcomepagefield id="'+$fieldLinkBusinessNumber.Id+'"></welcomepagefield>'
$sharedFileXML+='<welcomepagefield id="'+$fieldLinkERPNumber.Id+'"></welcomepagefield>'
$sharedFileXML+='<welcomepagefield id="'+$fieldLinkIndustry.Id+'"></welcomepagefield>'
$sharedFileXML+='<welcomepagefield id="'+$fieldLinkKAM.Id+'"></welcomepagefield>'
$sharedFileXMLSuffix='</wpf:welcomepagefields>'

$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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>