Export-Import SharePoint Content Type using PowerShell

Requirement: Copy a Content Type from the development environment to the staging environment.

Solution Overview:

  1. Export site columns associated with the content type from the source site collection
  2. Export custom content data types from source site collection
  3. Import site columns from the exported site columns XML
  4. Re-Create custom content types programmatically with PowerShell in the target site collection.

Steps in detail:
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 to XML using PowerShell
Once all fields of the particular content type are exported and imported, we can Export-Import Content types:

PowerShell Script to Export Content Type to XML:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Source Web
$SourceWeb = Get-SPWeb "https://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:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Target Web
$TargetWeb = Get-SPWeb "https://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: https://get-spscripts.com/2011/01/export-and-importcreate-site-columns-in.html

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “Export-Import SharePoint Content Type using PowerShell

  • This script works great on SP2013 and for normal content types on SP2016. However, a problem arises on SP2016 sites when dealing with a Document Set content type. Comparisons of XML files exported from a U.I. generated Document Set with an exported XML file from a Document Set created by Importing the XML from the U.I. generated document set reveals the latter XML introduces a Field ID for Name=”LinkFilenameNoMenu”. An approval workflow using the latter Document Set moves the document to the Drop Off library, but the Content Organizer timer job fails to move the Document Set to the Records Library. It gives the error
    “1 Document(s) were submitted to wkflowdev1 that are missing required properties or did not match any of the defined rules”. The error happens only for document set cts generated by the XML import not for cts generated by the User Interface. Has anyone else encountered this issue.

    Reply
  • Hi wanna create content type & Site columns from existing list in share site 2013

    Reply
  • I got this error while executing multiple times while testing for import content type. Error – Exception calling “.ctor” with “3” argument(s): “Unable to cast COM object of type ‘Microsoft.SharePoint.Library.SPRequestInternalClass’ to interface type ‘Microsoft.SharePoint.Library.ISPRequest’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{BDEADF28-C265-11D0-BCED-00A0C90AB50F}’ failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE)).”

    Reply
    • Hi, I’m getting the same error. Did you figure it out?

      Reply
  • Hey guy, just want to let you know that this code was just what I needed to migrate some content types to another site collection when we didn’t want to use the content hub. What a quick and elegant solution. Thanks again.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *