kwizcom banner advertisement

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()
{ 
    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 "IsSiteColumn" parameter value, this script can either add a site column or list column to given content type.
SharePoint Online: Add Column to List Content Type using PowerShell SharePoint Online: Add Column to List Content Type using PowerShell Reviewed by Salaudeen Rajack on 1:03 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.