Thursday 18 September 2014

Using the SPImport events to control SharePoint content import


The SPImport class in the Microsoft.SharePoint.Deployment namespace participates with other classes in the Deployment namespace to support importing, publishing, and migrating Windows SharePoint content, as well as supporting restore capabilities of content.
You can initiate an import operation by first initializing an instance of the Microsoft.SharePoint.Deployment.SPImportSettings class with the required import settings, and then passing the SPImportSettings object to the constructor of SPImport class; you then call the SPImport.Run method. The SPImport type exposes events that can be used to control the process of import. You can utilize the event model in PowerShell to register the events and then make use of this information to control the complete process of importing and exporting SharePoint content. Below is the PowerShell snippet to use the Error event and use the data to fail the execution process after an Import is performed.

$Global:importErrors = @()
$Global:importWarnings = @()  

try{      
     Register-ObjectEvent -InputObject $SPImport -EventName Error -SourceIdentifier Microsoft.SharePoint.Deployment.SPDeployment.Error -Action {
           $arguments = $event.SourceEventArgs
           if(($arguments.ErrorType -eq [Microsoft.SharePoint.Deployment.SPDeploymentErrorType]::Error) -or ($arguments.ErrorType -eq [Microsoft.SharePoint.Deployment.SPDeploymentErrorType]::FatalError)){
                $Global:importErrors += $arguments.ErrorMessage

           }
           if($arguments.ErrorType -eq [Microsoft.SharePoint.Deployment.SPDeploymentErrorType]::Warning){
                $Global:importWarnings += $arguments.ErrorMessage
           }

     } | Out-Null

     $SPImport.Run()
}
finally{

     Unregister-Event -SourceIdentifier Microsoft.SharePoint.Deployment.SPDeployment.Error -ErrorAction SilentlyContinue

} 

if($Global:importWarnings.Length -gt 0){
     Write-Warning ("SPImport generated {0} warnings during import process. Please refer to the SP-Import log file {1} for more details." -f $Global:importWarnings.Length, $logFile)
}

if($Global:importErrors.Length -gt 0){
     Write-Warning ("The below given errors were generated during import process. Please refer to the SP-Import log file {0} for more details." -f $logFile)
     $Global:importErrors | select -Unique | ForEach-Object {
           Write-Host $_
     }

     Write-Error ("SPImport failed with {0} errors during import process. The details of errors can be found in the SP-Import log file {1}" -f $Global:importErrors.Length, $logFile)
}

Write-Host ("SPImport process completed without errors. Please refer to the SP-Import log file {0} for more details" -f $logFile) -ForegroundColor Green

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()