Requirement: Copy a Content Type from development environment to staging environment.
Solution Overview:
To Export and Import Site columns associated with the particular content type, follow my post: Export Import Site Columns with PowerShell
Export Import SharePoint Content Type using PowerShell
Once all fields of the particular content type is exported and imported, we can Export-Import Content types:
PowerShell Script to Export Content Type:
PowerShell Script to Import Content Type:
Thanks to: http://get-spscripts.com/2011/01/export-and-importcreate-site-columns-in.html
Solution Overview:
- Export site columns associated with the content type from the source site collection
- Export custom content data types from source site collection
- Import site columns from the exported site columns xml
- Re-Create custom content types programmatically with PowerShell in the target site collection.
To Export and Import Site columns associated with the particular content type, follow my post: Export Import Site Columns with PowerShell
Export Import SharePoint Content Type using PowerShell
Once all fields of the particular content type is exported and imported, we can Export-Import Content types:
PowerShell Script to Export Content Type:
#Get the Source Web $SourceWeb = Get-SPWeb "http://dev.crescent.com" #Create Export XML File $XMLFile = "C:\SiteContentTypes.xml" New-Item $XMLFile -type file -force #Wrap Content Type Schema XML inside <ContentTypes> Element Add-Content $XMLFile "`n<ContentTypes>" #Export All Content types of specific Group to XML file $sourceWeb.ContentTypes | ForEach-Object { if ($_.Group -eq "Crescent Content Types") { #Export Content Types to XML file Add-Content $XMLFile $_.SchemaXml } } #Closing Wrapper Add-Content $XMLFile "</ContentTypes>" #Dispose the web object $SourceWeb.Dispose()
PowerShell Script to Import Content Type:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null #Get the Target Web $TargetWeb = Get-SPWeb "http://test.crescent.com" #Get the Content Type Schema from XML $XMLFile = "C:\SiteContentTypes.xml" [xml] $CTypeXML = Get-Content($XMLFile) #Create Site Content Types $CTypeXML.ContentTypes.ContentType | ForEach-Object { #Create New Content Type object inheriting from parent $SPContentType = New-Object Microsoft.SharePoint.SPContentType ($_.ID,$TargetWeb.ContentTypes,$_.Name) #Set Content Type description and group $SPContentType.Description = $_.Description $SPContentType.Group = $_.Group #Get all field References from the XML $_.Fields.Field | ForEach-Object { write-host $_.DisplayName #Add Fields Reference to the New content type if( !$SPContentType.FieldLinks[$_.DisplayName]) { #Create a field link for the Content Type by getting an existing column $SPFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($TargetWeb.Fields[$_.DisplayName]) #Check to see if column is 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 = $TargetWeb.ContentTypes.Add($SPContentType) $SPContentType.Update() write-host "Content type'" $ct.Name "'has been created" } $TargetWeb.Dispose()
Thanks to: http://get-spscripts.com/2011/01/export-and-importcreate-site-columns-in.html