kwizcom banner advertisement

SharePoint Online: Export-Import Site Columns using PowerShell

Requirement: Export-Import Site Columns in SharePoint Online.

SharePoint Online: Export Site Columns using PowerShell
#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
This PowerShell script exports all site columns from the given site column group to a XML file as below:
sharepoint online export site columns

Import Site Columns from the XML File using PowerShell:
#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 my another post on exporting and importing site columns in SharePoint On-premises using PowerShell: How to Export-Import Site Columns in SharePoint using PowerShell?
SharePoint Online: Export-Import Site Columns using PowerShell SharePoint Online: Export-Import Site Columns using PowerShell Reviewed by Salaudeen Rajack on September 29, 2017 Rating: 5

1 comment:

  1. 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.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.