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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • Hi Salaudeen,

    I had try to manage your script but I have this error:
    – Error Exporting Site Columns! Eccezione durante la chiamata di “ExecuteQuery” con “0” argomento/i: “URI non valido: impossibile analizzare il nome host.” –

    So I don’t understand what the script needs. Can you help me, please? Thanks
    Antimo

    Reply
    • Looks like there is something wrong with the URL! maybe some extra spaces, : or / characters.

      Reply
  • 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

Your email address will not be published. Required fields are marked *