SharePoint Online: Add Site Column to List using PowerShell

Requirement: Add an existing site column to the SharePoint list or library using PowerShell.

How to Add a Site Column to SharePoint Online List?

Site columns in SharePoint provide great re-usability without having to recreate the same columns multiple times! Once created at the top-level site, we can utilize them to store metadata in any number of lists and libraries under the hierarchy. To add a site column to the SharePoint list, follow these steps:

  1. Go to List Settings >> Under Columns, click on the “Add from existing site columns” link.
  2. From the available site columns, pick the required site column(s) and click on Add button.
    sharepoint online powershell to add site column to list
  3. Click OK to save your changes.

Let’s add a site column to the SharePoint Online list using PowerShell.

Add Site Column to List or Library with PowerShell:

This PowerShell CSOM script adds an existing site column “Department” to the ” Projects ” list.

#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"
 
#Variables
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$SiteColumnName="Department"

$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.web

#Get the list
$List=$Ctx.Web.Lists.GetByTitle($ListName)
    
#Get the Site column
$Field = $Web.Fields.GetByTitle($SiteColumnName)

#Add the site column to the list
$List.Fields.Add($Field)
$ctx.ExecuteQuery() 
    
Write-host "Site Column Added to the List Successfully!" -f Green

PowerShell to Add a Site Column to SharePoint List:

Let’s add some error handling to the above code to handle scenarios, such as:

  • What if the given site column doesn’t exist?
  • What if the given site column is already added to the list?
  • What if the given list doesn’t exist? Or what if the given credentials are invalid? Etc.
#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"
 
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$SiteColumnName="Department"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.web

    #Get the Site column from web
    $SiteColumns = $Web.Fields
    $Ctx.Load($SiteColumns)
    $Ctx.ExecuteQuery()
    $SiteColumn = $SiteColumns | Where {$_.Title -eq $SiteColumnName}
    
    #Check if given site column exists
    if($SiteColumn -eq $Null)
    {
        Write-host "Site Column $SiteColumnName doesn't exists!" -f Yellow
    }
    else
    {
        #Get the list
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Check if the Filed exist in list already
        $Fields = $List.Fields
        $Ctx.Load($List)
        $Ctx.Load($Fields)
        $Ctx.ExecuteQuery() 
        $Field = $Fields | where {$_.Title -eq $SiteColumnName}
        if($Field -ne $NULL)  
        {
            Write-host "Column Name $SiteColumnName already exists in the list!" -f Yellow
        }
        else
        {
            #Add the site column to the list
            $NewColumn = $List.Fields.Add($SiteColumn)
            $ctx.ExecuteQuery() 
    
            Write-host "Site Column Added to the List Successfully!" -f Green
        }
    }
}
Catch {
    write-host -f Red "Error Adding Site Column to List!" $_.Exception.Message
}

Here is another example:

#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 Add Fields to List 
Function Add-SPOSiteColumnToList([Microsoft.SharePoint.Client.List]$List, [GUID]$FieldID)
{
    #Get Fields from List
    $Ctx.Load($List.Fields)
    $Ctx.ExecuteQuery()
    
    #Get the Site Column from Web
    $SiteColumn = $List.ParentWeb.AvailableFields.GetById($FieldID)
    $Ctx.Load($SiteColumn)
    $Ctx.ExecuteQuery()

    #Check if the Field exist in list
    $ListField = $List.Fields | where {$_.ID -eq $SiteColumn.Id}
    if($ListField -eq $NULL)
    {
        #Add the site column to the list
        $NewColumn = $List.Fields.Add($SiteColumn)
        $ctx.ExecuteQuery()
        Write-host "Site Column '$($SiteColumn.Title)' Added to the List Successfully!" -f Green
    }
    else
    {
        Write-host "Site Column '$($SiteColumn.Title)' already exists in the list!" -f Yellow
    }
}
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Team Documents"

#Setup Credentials to connect
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()

#Add Rating Fields to List
$AverageRatingFieldID = [guid]"5a14d1ab-1513-48c7-97b3-657a5ba6c742"
$RatingCountFieldID = [guid]"b1996002-9167-45e5-a4df-b2c41c6723c7"

#Call the function to Add Site column to List
Add-SPOSiteColumnToList -List $List -FieldID $AverageRatingFieldID
Add-SPOSiteColumnToList -List $List -FieldID $RatingCountFieldID

PnP PowerShell To Add a Site Column to List

Here is how to add a site column to the SharePoint Online list using PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Team Projects"
$ColumnName= "ProjectStartDate" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Add Existing Site column to list
    Add-PnPField -List $ListName -Field $ColumnName -ErrorAction Stop
    Write-host "Site Column '$ColumnName' Added to List Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint Online PowerShell to Add Column to List
If you are looking for SharePoint Online PowerShell to add a column to a list, such as “Single Line of Text”, or “Choice”, etc. use :

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “SharePoint Online: Add Site Column to List using PowerShell

  • Thank you so much.
    The script fails if used in a subsite. The powershell fails to find the site columns located on the main site. Is there any way to fix it?

    Reply

Leave a Reply

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