October 30, 2013

Export& Import Content Type by powershell script

Export Content Type by power cell script:-

clear

   1:  write-host "Start..."
   2:  $sourceWeb = Get-SPWeb -Identity "http://br53:3000/"
   3:  write-host $sourceWeb.ContentTypes
   4:  #
   5:  $xmlFilePath = "C:\Script-SiteContentTypes.xml"
   6:  #
   7:  #Create Export File
   8:  New-Item $xmlFilePath -type file -force
   9:  #Export Content Types to XML file
  10:  Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
  11:  Add-Content $xmlFilePath "`n<ContentTypes>"
  12:  $sourceWeb.ContentTypes | ForEach-Object {
  13:      if ($_.Group -eq "Custom Content Types") {
  14:          Add-Content $xmlFilePath $_.SchemaXml
  15:      }
  16:  }
  17:  Add-Content $xmlFilePath "</ContentTypes>"
  18:  $sourceWeb.Dispose()

Import Content Type by power cell script:-


   1:  #http://fazlulchowdhury.blogspot.in/2012/02/how-to-importexport-custom-content.html
   2:  clear 
   3:  $destWeb = Get-SPWeb -Identity "http://br53:1001/"
   4:  write-host $destWeb
   5:  $xmlFilePath = "C:\Script-SiteContentTypes.xml"
   6:  ##Create Site Content Types
   7:  $ctsXML = [xml](Get-Content($xmlFilePath))
   8:  $ctsXML.ContentTypes.ContentType | ForEach-Object {
   9:      #Create Content Type object inheriting from parent
  10:      $spContentType = New-Object Microsoft.SharePoint.SPContentType ($_.ID,$destWeb.ContentTypes,$_.Name)
  11:      #Set Content Type description and group
  12:      $spContentType.Description = $_.Description
  13:      $spContentType.Group = $_.Group
  14:      $_.Fields.Field  | ForEach-Object {
  15:          if(!$spContentType.FieldLinks[$_.DisplayName])
  16:          {
  17:              #Create a field link for the Content Type by getting an existing column
  18:  $spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($destWeb.Fields[$_.DisplayName])
  19:              #Check to see if column should be Optional, Required or Hidden
  20:              if ($_.Required -eq "TRUE") {$spFieldLink.Required = $true}
  21:              if ($_.Hidden -eq "TRUE") {$spFieldLink.Hidden = $true}
  22:              #Add column to Content Type
  23:              $spContentType.FieldLinks.Add($spFieldLink)
  24:          }
  25:      }
  26:      #Create Content Type on the site and update Content Type object
  27:      $ct = $destWeb.ContentTypes.Add($spContentType)
  28:      $spContentType.Update()
  29:      write-host "Content type" $ct.Name "has been created"
  30:  }
  31:  $destWeb.Dispose()


If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment