SharePoint Online: Add Column to List Content Type using PowerShell

Requirement: Add field to list content type in SharePoint Online.

How to Add a column to a List or Library Content type in SharePoint Online?
If you want to add a field to list or library's content type, follow these steps:
  • Go to the list or library settings >> Click on the appropriate content type name from List settings page.
  • In List Content Type page, under columns section, click on "Add from existing site or list columns" link
  • Select the appropriate column group and then select the column you want to add to content type, Click on "Add" button and then OK to add that column to your content type.
    sharepoint online powershell add field to list content type
You can add column either from the existing list's columns or from site columns.

SharePoint Online: PowerShell to Add Field to List Content Type
Lets add field to list content type 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 Add-ColumnToListContentType()
        [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)

        #Get the content type from list
        $ContentTypeColl = $List.ContentTypes
        #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

        #Get the column to add: Either site column or existing list's column
            $ColumnColl = $Ctx.Web.Fields
        else #List Column
            $ColumnColl = $List.Fields
        $Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
        #Check if given column exists
        if($Column -eq $Null)
            Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
            #Check if column already added to the content type
            $FieldCollection = $ContentType.Fields
            $Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
            if($Field -ne $Null)
                Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
            #Add field to content type
            $FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $FieldLink.Field = $Column
            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
$ColumnName="Head Count"

#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName 
Based on "IsSiteColumn" parameter value, this script can either add a site column or list column to given content type.

If you need to add a field to site content type, use: SharePoint Online: How to Add a Site Column to Content Type using Powershell?

1 comment:

  1. Hi I am not able to get this to pull in the site column. Any idea why not?


Please Login and comment to get your questions answered!

Powered by Blogger.