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 great reusability without having to recreate same columns multiple times! Once created, we can utilize them in any number of lists and libraries. To add a site column to SharePoint list, follow these steps:
Add Site Column to List or Library with PowerShell:
How to Add a Site Column to SharePoint Online List?
Site columns in SharePoint provides great reusability without having to recreate same columns multiple times! Once created, we can utilize them in any number of lists and libraries. 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.
- Click OK to save your changes.
Add Site Column to List or Library with PowerShell:
#Load SharePoint CSOM AssembliesLets add some error handling to the above code to handle scenarios, such as:
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
- 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
}