SharePoint Online: How to Add a Site Column to Content Type using Powershell?
Requirement: Add a site column to content type in SharePoint Online using PowerShell.
How to add field to existing content type in SharePoint?
By default Title column gets added to the content type. However, you may want to add additional columns.
PowerShell to Add Site Column to Content Type in SharePoint Online:
Lets add column to existing content type programmatically with PowerShell.
PnP PowerShell to Add Site Column to Content Type
Here is how to add site column to content type in SharePoint Online using PnP PowerShell
How to add field to existing content type in SharePoint?
By default Title column gets added to the content type. However, you may want to add additional columns.
- Go to the SharePoint Online site where you want Add a column to site content type. Click on Settings >> Click Site Settings.
- In Site Settings, Click on Site content types under Web Designer Galleries section
- Select your target content type to modify. In site content types page, under Columns section, click on "Add from existing site columns" link.
- This takes you to a page where you can select any 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.
PowerShell to Add Site Column to Content Type in SharePoint Online:
Lets add column to 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
PnP PowerShell to Add Site Column to Content Type
Here is how to add site column to content type in SharePoint Online using PnP PowerShell
#Config Variables $SiteURL = "https://crescenttech.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 }
No comments:
Please Login and comment to get your questions answered!