How to Import/Export Custom Content Types in SharePoint 2010 using Powershell

Let us first create two files of which the file names and content will be as follows:

1. File Name: CTypes.ps1. This file contains the script to export existing site content types (From: http://apps-d/myApp and Content types group: My Custom Content Types ) into an XML script (To: D:\Development\SharePoint\MYAPP\Scripts\Script-SiteContentTypes.xml)

$sourceWeb = Get-SPWeb http://apps-d/myApp
$xmlFilePath = "C:\Script-SiteContentTypes.xml"

#Create Export File
New-Item $xmlFilePath -type file -force

#Export Content Types to XML file
Add-Content $xmlFilePath ""
Add-Content $xmlFilePath "`n"
$sourceWeb.ContentTypes ForEach-Object {
if ($_.Group -eq "My Custom Content Types") {
Add-Content $xmlFilePath $_.SchemaXml
}
}
Add-Content $xmlFilePath "
"

$sourceWeb.Dispose()

2. File Name: Import.ps1. This file contains the script to import the content types into http://206137l/MyApp from XML script (To: C:\Script-SiteContentTypes.xml)

$destWeb = Get-SPWeb http://206137l/MyApp
$xmlFilePath = "D:\Development\SharePoint\MYAPP\Scripts\Script-SiteContentTypes.xml"
#Create Site Content Types
$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()

3. Then open up SharePoint 2010 Management Shell in Administrative mode and change the directory to the script folder (i.e. D:\Development\SharePoint\MYAPP\Scripts). Then, run those scripts together with the folder location (i.e. D:\Development\SharePoint\MYAPP\Scripts\ CTypes.ps1 and D:\Development\SharePoint\MYAPP\Scripts\ Import.ps1)

Then, if we go to the new site, we will see that the content types have been imported successfully.

Comments

  1. Thanks for the post. BUT I'm gettinf below while running the post.
    Unexpected token 'ForEach-Object' in expression or statement.
    At D:\CTypes.ps1:10 char:39
    + $sourceWeb.ContentTypes ForEach-Object <<<< {
    + CategoryInfo : ParserError: (ForEach-Object:String) [], ParseEx
    ception
    + FullyQualifiedErrorId : UnexpectedToken

    ReplyDelete
  2. There's only missing a pipe, it has to be "$sourceWeb.ContentTypes | ForEach-Object"

    ReplyDelete
  3. I got the below error when i try to import the custom content type.

    Exception setting "Group": "The Group property cannot be set to the empty strin
    g. To use the default, set Group to null."
    At C:\ContentTypeMigration\Import.ps1:12 char:16
    + $spContentType. <<<< Group = $_.Group
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    Index operation failed; the array index evaluated to null.
    At C:\ContentTypeMigration\Import.ps1:15 char:31
    + if(!$spContentType.FieldLinks[ <<<< $_.DisplayName])
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex

    Exception calling "Add" with "1" argument(s): "A duplicate content type "Item"
    was found."
    At C:\ContentTypeMigration\Import.ps1:30 char:32
    + $ct = $destWeb.ContentTypes.Add <<<< ($spContentType)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    Exception calling "Update" with "0" argument(s): "A content type which does not
    belong to a collection cannot be updated."
    At C:\ContentTypeMigration\Import.ps1:31 char:22
    + $spContentType.Update <<<< ()
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    Content type has been created

    ReplyDelete
  4. You should link to your code sources. Your code seems to be an exact copy from this website: http://get-spscripts.com/2011/02/export-and-importcreate-site-content.html

    ReplyDelete
  5. you have copied the code from http://get-spscripts.com/2011/02/export-and-importcreate-site-content.html.

    ReplyDelete

Post a Comment

Popular posts from this blog

Cloud Computing Technology Assessment

Database Testing With DBUnit

Data Science, Big Data & Microsoft Machine Learning