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:
[sourcecode language=”csharp”]
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 = ‘
#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 “
#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=’
$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)
[/sourcecode]
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.
Leave a Reply