Export-Import Site Columns in SharePoint using PowerShell

It’s a frequent requirement to copy site columns between SharePoint environments, isn’t it? Today had a requirement to copy a bunch of site columns from the development environment to the staging environment.

We used to package site columns as a WSP Solution package in such requirements: Create Site Column Feature for SharePoint 2010, This time, let’s use PowerShell to Export and Import Site Columns!

PowerShell Script to Export Site columns:

#Get the Source Web
$sourceWeb = Get-SPWeb "http://dev.crescent.com"

#Create a XML File to Export Fields
$xmlFile = "C:\SiteColumns.xml"
New-Item $xmlFile -type file -force

#Wrap Field Schema XML inside <Fields> Element 
Add-Content $xmlFile "`n<Fields>"

#Export All Site Columns of specific Group to XML file
$sourceWeb.Fields | ForEach-Object {
	if ($_.Group -eq "Crescent Travel Request") {
		Add-Content $xmlFile $_.SchemaXml
	}
}
#Closing Wrapper
Add-Content $xmlFile "</Fields>"

#Dispose the web object
$sourceWeb.Dispose()

PowerShell Script to Import Site Columns from XML:

#Get the Target Web
$TargetWeb = Get-SPWeb "http://test.crescent.com"

#Get XML file exported
[xml]$fieldsXML = Get-Content("C:\SiteColumns.xml")

#Loop Through Each Field
$fieldsXML.Fields.Field | ForEach-Object {
	#Check if the target web has the field already!
	if ($TargetWeb.Fields.ContainsField( $_.Name) -Eq $false){ 
		#Add Site column to Target Web
		$TargetWeb.fields.AddFieldAsXml($_.OuterXml)
	}
}

Thanks Phil for the idea: http://get-spscripts.com/2011/01/export-and-importcreate-site-columns-in.html

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

10 thoughts on “Export-Import Site Columns in SharePoint using PowerShell

  • September 26, 2016 at 9:26 AM

    Thank you a million!
    I sincerely appreciate your help.

    Reply
  • October 30, 2014 at 9:53 AM

    Thank you.
    You didn’t dispose web in import script 😉

    #Dispose the web object
    $TargetWeb.Dispose()

    Reply
  • October 28, 2014 at 1:02 PM

    Exactly what I was looking for!!

    Reply
  • August 12, 2014 at 4:04 PM

    Thank you so much Salaudeen!! I tried $_.Name didn’t work I tried $_.DisplayName didn’t work!
    You are the Best StaticName did work!!!

    Appreciate your help.
    Regards,
    Khushi

    Reply
  • August 11, 2014 at 10:45 PM

    How can I restrict the export only the selected column from the group? Not all the columns of the entire group?

    Reply
  • August 11, 2014 at 10:58 PM

    I did try to put the if {$_.Name -eq “ColumnName” } within the if ($_.Group -eq “GroupName”). But it is not exporting the selected column.

    Reply
    • August 12, 2014 at 2:19 PM

      Khushi,

      There is no “Name” property in SPField class. Use properties such as: Title, StaticName, InternalName.

      Reply
  • July 16, 2014 at 7:33 AM

    thanks, the only script that worked for me 🙂

    Reply
  • April 30, 2013 at 3:16 PM

    Does not work for me… Empty XML…

    Reply
    • April 30, 2013 at 3:26 PM

      Check your Site columns Group! I’ve used “Crescent Travel Request”, Update it accordingly!!

      Reply

Leave a Reply