SharePoint Online: Add Column to List Content Type using PowerShell
Requirement: Add a field to list content type in SharePoint Online.
How to Add a column to a List or Library Content type in SharePoint Online?
PowerShell is a powerful scripting language that can help automate many tasks in SharePoint. There may be times when you need to add a column to a list content type. You could do this manually, but it can be time-consuming if you have lots of lists and content types. This blog post will show you how to add a column to a SharePoint Online list content type using PowerShell.
If you want to add a field to a list or library’s content type, follow these steps:
- Go to the list or library settings >> Click on the appropriate content type name from the list settings page.
- On the List Content Type page, under the columns section, click on the “Add from existing site or list columns” link
- Select the appropriate column group and then select the column you want to add to the content type, click on the “Add” button, and then OK to add that column to your content type.
You can add columns either from the existing list’s columns or from site columns.
SharePoint Online: PowerShell to Add Field to List Content Type
Let’s add a field to list content type using PowerShell. Here is how to use PowerShell to add a column to a list content type:
#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 Add-ColumnToListContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ContentTypeName,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$false)] [bool] $IsSiteColumn=$True
)
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
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get the content type from list
$ContentTypeColl = $List.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Check if the content type exists in the list
$ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
If($ContentType -eq $Null)
{
Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow
Return
}
#Get the column to add: Either site column or existing list's column
If($IsSiteColumn)
{
$ColumnColl = $Ctx.Web.Fields
}
else #List Column
{
$ColumnColl = $List.Fields
}
$Ctx.Load($ColumnColl)
$Ctx.ExecuteQuery()
$Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
#Check if given column exists
if($Column -eq $Null)
{
Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
Return
}
else
{
#Check if column already added to the content type
$FieldCollection = $ContentType.Fields
$Ctx.Load($FieldCollection)
$Ctx.ExecuteQuery()
$Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
if($Field -ne $Null)
{
Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
Return
}
#Add field to content type
$FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$FieldLink.Field = $Column
[Void]$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update($false)
$Ctx.ExecuteQuery()
Write-host "Column '$ColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Projects"
$ColumnName="Head Count"
$IsSiteColumn=$false
#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName
Based on the “IsSiteColumn” parameter value, this script can either add a site column or list column to a given content type.
PnP PowerShell to Add a Column to List Content Type in SharePoint Online
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName = "Crescent Project V2"
$FieldName = "Department"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Content Type from the List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
#Add a column list
$Field = Add-PnPField -List $ListName -DisplayName $FieldName -InternalName $FieldName -Type Choice -Choices "Sales","Marketing","Purchase" -AddToDefaultView
#Add the column to Content type
Add-PnPFieldToContentType -Field $Field -ContentType $ContentType
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
If you need to add a field to the site content type, use: SharePoint Online: How to Add a Site Column to Content Type using PowerShell?
Hi I am not able to get this to pull in the site column. Any idea why not?