Export and import/create site content types in SharePoint using PowerShell
As described previously, SharePoint Server 2010 does contain the new Content Type Hub feature for distributing site columns and content types across multiple site collections, but there still may be scenarios where the feature cannot be used – e.g., if using SharePoint Foundation, migrating columns and content types between farms, or for documenting an environment.
As with the previous site column article, we can use PowerShell to export the content type schema from a source site collection into an XML file, which can then be used to import the same content types into another site collection.
Please note (Part 1): The script used here assumes that you have already migrated the site columns used in the these content types to the destination site collection. You could, of course, write a combined script that covers exporting/importing both site columns and content types to ensure the whole process is dealt with as a one-click operation.
Please note (Part 2): The script only supports creating the content type in a destination site and associating the appropriate site columns to it. It does not copy across other features of the content type – e.g., workflow associations, information management policies, document template, etc. – although you could expand the script to support extra features, if required.
For this example, I have created the following site content types under the “Custom Content Types” group in my development site collection:
I have also added a required “Description” column to the “Company Document” content type (from which the other custom content types inherit) and an optional “Category” column to the “IT Document” content type.
The PowerShell script below will export these content types to an XML file called Script-SiteContentTypes.xml in the C:\Install folder. Note that the script looks at the group called “Custom Content Types” to ensure content types created by a standard SharePoint installation are not included in the export. You will need to ensure that appropriate custom group name(s) are specified in your script when exporting:
$sourceWeb = Get-SPWeb http://portalThis script will generate an XML file similar to the one shown below.
$xmlFilePath = "C:\Install\Script-SiteContentTypes.xml"
#Create Export File
New-Item $xmlFilePath -type file -force
#Export Content Types to XML file
Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $xmlFilePath "`n<ContentTypes>"
$sourceWeb.ContentTypes | ForEach-Object {
if ($_.Group -eq "Custom Content Types") {
Add-Content $xmlFilePath $_.SchemaXml
}
}
Add-Content $xmlFilePath "</ContentTypes>"
$sourceWeb.Dispose()
Once you have the XML file, it can be used to import the content types into another site collection by using the script below. The first part of the script gets the destination Web URL and exported XML file:
$destWeb = Get-SPWeb http://portal/sites/migrationtestThe final part of the script cycles through each content type specified in the XML file, creates the content type with the exported description and group name, and finally reads in each column associated with the exported content type and wires them up to the new content type in the destination site collection, specifying if they are hidden or required columns during the process.
$xmlFilePath = "C:\Install\Script-SiteContentTypes.xml"
#Create Site Content TypesOnce the script has been run, the custom site content types exported from the original site collection will be present in the new site collection…
$ctsXML = [xml](Get-Content($xmlFilePath))
$ctsXML.ContentTypes.ContentType | ForEach-Object {
#Create Content Type object inheriting from parent
$spContentType = New-Object Microsoft.SharePoint.SPContentType ($_.ID,$destWeb.ContentTypes,$_.Name)
#Set Content Type description and group
$spContentType.Description = $_.Description
$spContentType.Group = $_.Group
$_.Fields.Field | ForEach-Object {
if(!$spContentType.FieldLinks[$_.DisplayName])
{
#Create a field link for the Content Type by getting an existing column
$spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($destWeb.Fields[$_.DisplayName])
#Check to see if column should be Optional, Required or Hidden
if ($_.Required -eq "TRUE") {$spFieldLink.Required = $true}
if ($_.Hidden -eq "TRUE") {$spFieldLink.Hidden = $true}
#Add column to Content Type
$spContentType.FieldLinks.Add($spFieldLink)
}
}
#Create Content Type on the site and update Content Type object
$ct = $destWeb.ContentTypes.Add($spContentType)
$spContentType.Update()
write-host "Content type" $ct.Name "has been created"
}
$destWeb.Dispose()
…with the “Description” and “Category” columns linked to them as expected.
No comments:
Post a Comment