SharePoint Online: How to Add a Site Column to Content Type using PowerShell?

Requirement:  Add a site column to a content type in SharePoint Online using PowerShell.

How to add a field to an existing content type in SharePoint?

Site columns are reusable fields that can be added to content types, lists, and libraries. By adding site columns to your content types, you can make creating and managing your information easier. In this blog post, we’ll walk you through the process of adding a site column to a content type in SharePoint Online. We will also provide you with a PowerShell script that you can use to add a site column to a content type in SharePoint Online.

By default, the Title column gets added to the content type. However, you may want to add additional columns.

  1. Go to the SharePoint Online site where you want to add a column to site content type.
  2. Click on “Settings” gear >> Click “Site Settings”. 
  3. In Site Settings, Click on “Site content types” under Web Designer Galleries section
  4. Select your target content type to modify. In site content types page, under Columns section, click on “Add from existing site columns” link.
    sharepoint online add column to content type powershell
  5. This takes you to a page where you can select an existing site column. Select the appropriate group and pick the site column you want to add, and then click “Add” to move the column to the “Columns to add” list.
    sharepoint online add column to content type powershell

Let’s see how to add a column to a content type in SharePoint Online using PowerShell.

PowerShell to Add Site Column to Content Type in SharePoint Online:

PowerShell is a great way to automate tasks in SharePoint Online. Let’s add a column to the existing content type programmatically 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"

Function Add-SiteColumnToContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $SiteColumnName
    )

    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 content type
        $ContentTypeColl = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the site        
        $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($ContentType -eq $Null)
        { 
            Write-host "Content Type '$ContentTypeName' doesn't exists in  '$SiteURL'" -f Yellow
            Return 
        }
        #Get the site column to add
        $SiteColumnColl = $Ctx.Web.Fields
        $Ctx.Load($SiteColumnColl)
        $Ctx.ExecuteQuery()
        $SiteColumn = $SiteColumnColl | Where {$_.Title -eq $SiteColumnName}
    
        #Check if given site column exists
        if($SiteColumn -eq $Null)
        {
            Write-host "Site Column '$SiteColumnName' doesn't exists!" -f Yellow
            Return
        }
        else
        {
            #Check if site column already added to the content type
            $FieldCollection = $ContentType.Fields
            $Ctx.Load($FieldCollection)
            $Ctx.ExecuteQuery()
            $Field = $FieldCollection | Where {$_.Title -eq $SiteColumnName}
            if($Field -ne $Null)
            {
                Write-host "Site Column '$SiteColumnName' Already Exists in the content type!" -f Yellow
                Return
            }
    
            #Add a field to content type
            $FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $FieldLink.Field = $SiteColumn
            [Void]$ContentType.FieldLinks.Add($FieldLink)
            $ContentType.Update($true)
            $Ctx.ExecuteQuery() 
       
            Write-host "Site Column '$SiteColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
        }
   }
    Catch {
        write-host -f Red "Error Adding Site Column to Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="ProjectTemplate"
$SiteColumnName="Project Title"

#Call the function
Add-SiteColumnToContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName

This can be helpful if you need to add the same column to many content types.

PnP PowerShell to Add Site Column to Content Type

Here is how to add a site column to a content type in SharePoint Online using PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName = "Crescent Project V2"
$SiteColumnName = "Project_x0020_Manager" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Add Field to Content type
    Add-PnPFieldToContentType -Field $SiteColumnName -ContentType $ContentTypeName
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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!

Leave a Reply

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