Thursday, 18 September 2014

Export and import content types using PowerShell

SharePoint stores the definition schema of content types in an XML file that creates a content type in the element manifest file of a Feature. You can use the information present in the SchemaXML of a content type to migrate it between site collections using PowerShell.
Below is the screen shot of the schema definition of the Article Page content type as from the SharePoint manager application.


You can use this information to export the content type to an XML file as given below. The script expects the site collection url, group name under which the content types should be exported and the output directory location.

param ([string]$SiteUrl = 'http://yoursiteurl',[string] $GroupName = "Your Group Name", [string]$OutPutLocation = "E:\Powershell\SharePoint")
 
$ErrorActionPreference = 'Stop'
If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
{
     Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
}
$site = Get-SPSite $SiteUrl
$web = $site.RootWeb
if([System.IO.Directory]::Exists($OutPutLocation) -ne $true)
{
     Write-Error "Export path " $OutPutLocation " is not valid. Make sure that the directory exists"
}
$fileName = [System.IO.Path]::Combine($OutPutLocation, "content-types.xml")
Write-Host "Starting content type export to $fileName" -ForegroundColor Green
if([System.IO.File]::Exists($fileName) -ne $true)
     New-Item -path $OutPutLocation -name "content-types.xml" -type File
}
Add-Content $fileName "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $fileName "`n<ContentTypes>"
$web.ContentTypes | ForEach-Object {
   if ($_.Group -eq $GroupName) {
           Write-Host -ForegroundColor Blue $_.Name
        Add-Content $fileName $_.SchemaXml
   }
}
Add-Content $fileName "</ContentTypes>"
$web.Dispose()

The exported data looks much simpler as given below.

Later you can use this data to import the content types back to another site collection or the same site collection as given below.
param([string] $SiteUrl = "http://yoursiteurl", [string]$OutPutLocation = "E:\Powershell\SharePoint")

$ErrorActionPreference = 'Stop'
If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
{
     Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
}
$site = Get-SPSite $SiteUrl
$web = $site.RootWeb
$fileName = [System.IO.Path]::Combine($OutPutLocation, "content-types.xml")
if([System.IO.File]::Exists($fileName) -ne $true){
     Write-Error "The content type export file $fileName does not exist"
}
$contentTypeExport = [xml](Get-Content($fileName))
$contentTypeExport.ContentTypes.ContentType | ForEach-Object {

     $name = $_.Name
     $contentType = $web.ContentTypes | Where-Object { $_.Name -eq $name }
     if($contentType -eq $null){
         $spContentType = New-Object Microsoft.SharePoint.SPContentType ($_.ID,$web.ContentTypes,$_.Name)
         $spContentType.Description = $_.Description
         $spContentType.Group = $_.Group
  
         $_.Fields.Field  | ForEach-Object {
             if(!$spContentType.FieldLinks[$_.DisplayName])
             {
                     $field = $web.Fields[$_.DisplayName]
                     if($field -ne $null){
                      $spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($web.Fields[$_.DisplayName])
                       if ($_.Required -eq "TRUE") {$spFieldLink.Required = $true}
                       if ($_.Hidden -eq "TRUE") {$spFieldLink.Hidden = $true}
                       $spContentType.FieldLinks.Add($spFieldLink)
                     }
             }
         }
         $newContentType = $web.ContentTypes.Add($spContentType)
         $spContentType.Update()
         Write-Host "Successfully imported content type $name" -ForegroundColor Green
     }  
     else{     
         Write-Host "Content type $name already exists" -ForegroundColor Yellow
     }
}
$web.Dispose()

No comments:

Post a Comment