SharePoint Online: Export-Import Site Columns using PowerShell

Requirement: Export-Import Site Columns in SharePoint Online.

SharePoint Online: Export Site Columns using PowerShell

So, you have a lot of Site Columns in your SharePoint Online environment. And, need to export and import those Site Columns between environments? Doing this through a web browser interface can be tedious, so I wrote some PowerShell scripts that make the process much easier. In this post, I’ll show you how to export and import Site Columns between SharePoint Online sites (or between different SharePoint Online Tenants) using PowerShell.

This PowerShell script exports all site columns of a specific group for the SharePoint Online site to an XML file:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to export site columns from a group
Function Export-SPOSiteColumns([String]$SiteURL, [String]$SiteColumnGroup,[String]$XMLFile)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Get All Site Columns of the Web
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Fields)
        $Ctx.ExecuteQuery()

        #Get the site column Group
        $SiteColumns = $Ctx.Web.Fields | where {$_.Group -eq $SiteColumnGroup}
        if($SiteColumns -ne $NULL)
        {
            #Wrap Field Schema XML inside <Fields> Element 
            Add-Content $XMLFile "`n<Fields>"

            #Loop through each site column of the group
            ForEach($Field in $SiteColumns)
            {
                Add-Content $XMLFile $Field.SchemaXml
            }
            #Closing Wrapper
            Add-Content $XMLFile "</Fields>"

            Write-host -f Green "Site Columns Exported Successfully!"
        }
        else
        {
            Write-host -f Yellow "Could not find the Site Column Group '$($SiteColumnGroup)' at '$($SiteURL)'"
        }
    }
    Catch {
    write-host -f Red "Error Exporting Site Columns!" $_.Exception.Message
    }
}

#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$SiteColumnGroup="Crescent Projects"
$XMLFile="C:\Temp\CrescentSiteColumns.xml"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to export the Site Column group to XML
Export-SPOSiteColumns $SiteURL $SiteColumnGroup $XMLFile

Once executed, this PowerShell script exports all site columns from the given site column group to an XML file as below:

sharepoint online export site columns

Import Site Columns from the XML File using PowerShell:

The next step is to import them into another SharePoint Online environment or site.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Import site columns from a XML file
Function Import-SPOSiteColumns([String]$SiteURL, [String]$XMLFile)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Get XML file contents
        [xml]$FieldsXML = Get-Content($XMLFile)

        #Remove the "Version" Attribute from the XML
        $FieldsXML.Fields.Field.RemoveAttribute("Version")

        #Loop Through Each Field
        ForEach($Field in $FieldsXML.Fields.Field)
        {
            #Check if the web has the field already!
            $Ctx.Load($Ctx.Web.Fields)
            $Ctx.ExecuteQuery()

            $SiteColumn = $Ctx.Web.Fields | Where {$_.InternalName -eq $Field.Name}
            If($SiteColumn -eq $Null)
            {
                #Add Site column to Web
                $SiteColumn = $Ctx.Web.Fields.AddFieldAsXml($Field.OuterXml, $True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
                $Ctx.ExecuteQuery()
                Write-host "Site Column Added:" $($Field.Name)
            }
            Else
            {
                Write-host -f Yellow "Site Column '$($Field.Name)' Already Exists!"
            }
        }
        Write-host -f Green "Site Columns Imported Successfully!"
    }
    Catch {
    write-host -f Red "Error Importing Site Columns!" $_.Exception.Message
    }
}

#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$XMLFile="C:\Temp\CrescentSiteColumns.xml"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to Import the Site Columns from XML
Import-SPOSiteColumns $SiteURL $XMLFile 

Here is another post on exporting and importing site columns in SharePoint On-premises using PowerShell: How to Export-Import Site Columns in SharePoint using PowerShell?

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

4 thoughts on “SharePoint Online: Export-Import Site Columns using PowerShell

  • any version of this script for MFA (im too beginner to arrange it for MFA) ? it doesnt work for me 🙁 Thank you for your great work

    Reply
  • Thanks – Saved me hours of manual work

    Reply
  • Thanks! Worked great with SP online.

    Reply
  • Thank you for this valuable post! I would like to thank you personally for all your posts and this blog helped me in many situations.

    Reply

Leave a Reply