SharePoint Online: Add Site Column to List using PowerShell

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

How to Add a Site Column to SharePoint Online List?
Site columns in SharePoint provides a 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 SharePoint list, follow these steps:
  • Go to List Settings >> Under Columns, click on "Add from existing site columns" link.
  • 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
  • Click OK to save your changes.

Add Site Column to List or Library with 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"
 
#Variables
$SiteURL="https://crescent.sharepoint.com"
$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 an 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:
#Config Variables
$SiteURL = "https://crescenttech.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 list, such as "Single Line of Text", "Choice", etc. use :

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.