October 30, 2013

Export & Import Site Columns by power script

Export Site Columns by power script:


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

Import Site Columns by power script:

clear

   1:  $destWeb = Get-SPWeb -Identity "http://br53:1001/"
   2:  $installPath = "C:"
   3:  #Get exported XML file
   4:  $fieldsXML = [xml](Get-Content($installPath + "\Script-SiteColumns.xml"))
   5:  $fieldsXML.Fields.Field | ForEach-Object {
   6:      #Configure core properties belonging to all column types
   7:      $fieldXML = '<Field Type="' + $_.Type + '"
   8:      Name="'
+ $_.Name + '"
   9:      ID="'
+ $_.ID + '"
  10:      Description="'
+ $_.Description + '"
  11:      DisplayName="'
+ $_.DisplayName + '"
  12:      StaticName="'
+ $_.StaticName + '"
  13:      Group="'
+ $_.Group + '"
  14:      Hidden="'
+ $_.Hidden + '"
  15:      Required="'
+ $_.Required + '"
  16:      Sealed="'
+ $_.Sealed + '"'
  17:      #Configure optional properties belonging to specific column types – you may need to add some extra properties here if present in your XML file
  18:      if ($_.ShowInDisplayForm) { $fieldXML = $fieldXML + "`n" + 'ShowInDisplayForm="' + $_.ShowInDisplayForm + '"'}
  19:      if ($_.ShowInEditForm) { $fieldXML = $fieldXML + "`n" + 'ShowInEditForm="' + $_.ShowInEditForm + '"'}
  20:      if ($_.ShowInListSettings) { $fieldXML = $fieldXML + "`n" + 'ShowInListSettings="' + $_.ShowInListSettings + '"'}
  21:      if ($_.ShowInNewForm) { $fieldXML = $fieldXML + "`n" + 'ShowInNewForm="' + $_.ShowInNewForm + '"'}
  22:      if ($_.EnforceUniqueValues) { $fieldXML = $fieldXML + "`n" + 'EnforceUniqueValues="' + $_.EnforceUniqueValues + '"'}
  23:      if ($_.Indexed) { $fieldXML = $fieldXML + "`n" + 'Indexed="' + $_.Indexed + '"'}
  24:      if ($_.Format) { $fieldXML = $fieldXML + "`n" + 'Format="' + $_.Format + '"'}
  25:      if ($_.MaxLength) { $fieldXML = $fieldXML + "`n" + 'MaxLength="' + $_.MaxLength + '"' }
  26:      if ($_.FillInChoice) { $fieldXML = $fieldXML + "`n" + 'FillInChoice="' + $_.FillInChoice + '"' }
  27:      if ($_.NumLines) { $fieldXML = $fieldXML + "`n" + 'NumLines="' + $_.NumLines + '"' }
  28:      if ($_.RichText) { $fieldXML = $fieldXML + "`n" + 'RichText="' + $_.RichText + '"' }
  29:      if ($_.RichTextMode) { $fieldXML = $fieldXML + "`n" + 'RichTextMode="' + $_.RichTextMode + '"' }
  30:      if ($_.IsolateStyles) { $fieldXML = $fieldXML + "`n" + 'IsolateStyles="' + $_.IsolateStyles + '"' }
  31:      if ($_.AppendOnly) { $fieldXML = $fieldXML + "`n" + 'AppendOnly="' + $_.AppendOnly + '"' }
  32:      if ($_.Sortable) { $fieldXML = $fieldXML + "`n" + 'Sortable="' + $_.Sortable + '"' }
  33:      if ($_.RestrictedMode) { $fieldXML = $fieldXML + "`n" + 'RestrictedMode="' + $_.RestrictedMode + '"' }
  34:      if ($_.UnlimitedLengthInDocumentLibrary) { $fieldXML = $fieldXML + "`n" + 'UnlimitedLengthInDocumentLibrary="' + $_.UnlimitedLengthInDocumentLibrary + '"' }
  35:      if ($_.CanToggleHidden) { $fieldXML = $fieldXML + "`n" + 'CanToggleHidden="' + $_.CanToggleHidden + '"' }
  36:      if ($_.List) { $fieldXML = $fieldXML + "`n" + 'List="' + $_.List + '"' }
  37:      if ($_.ShowField) { $fieldXML = $fieldXML + "`n" + 'ShowField="' + $_.ShowField + '"' }
  38:      if ($_.UserSelectionMode) { $fieldXML = $fieldXML + "`n" + 'UserSelectionMode="' + $_.UserSelectionMode + '"' }
  39:      if ($_.UserSelectionScope) { $fieldXML = $fieldXML + "`n" + 'UserSelectionScope="' + $_.UserSelectionScope + '"' }
  40:      if ($_.BaseType) { $fieldXML = $fieldXML + "`n" + 'BaseType="' + $_.BaseType + '"' }
  41:      if ($_.Mult) { $fieldXML = $fieldXML + "`n" + 'Mult="' + $_.Mult + '"' }
  42:      if ($_.ReadOnly) { $fieldXML = $fieldXML + "`n" + 'ReadOnly="' + $_.ReadOnly + '"' }
  43:      if ($_.FieldRef) { $fieldXML = $fieldXML + "`n" + 'FieldRef="' + $_.FieldRef + '"' }   
  44:      $fieldXML = $fieldXML + ">"
  45:      #Create choices if choice column
  46:      if ($_.Type -eq "Choice") {
  47:          $fieldXML = $fieldXML + "`n<CHOICES>"
  48:          $_.Choices.Choice | ForEach-Object {
  49:             $fieldXML = $fieldXML + "`n<CHOICE>" + $_ + "</CHOICE>"
  50:          }
  51:          $fieldXML = $fieldXML + "`n</CHOICES>"
  52:      }
  53:       #Set Default value, if specified  
  54:      if ($_.Default) { $fieldXML = $fieldXML + "`n<Default>" + $_.Default + "</Default>" }
  55:      #End XML tag specified for this field
  56:      $fieldXML = $fieldXML + "</Field>"
  57:       #Create column on the site
  58:      $destWeb.Fields.AddFieldAsXml($fieldXML.Replace("&","&amp;"))
  59:      write-host "Created site column" $_.DisplayName "on" $destWeb.Url
  60:      $destWeb.Dispose()
  61:  }​

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

No comments:

Post a Comment