PowerShell to Add-Remove Fields to Content Type in SharePoint
If you have a requirement to add a field to a content type or remove a field from the existing content type, use this PowerShell script:
PowerShell script to Add a field to content type:
#Get Web Object
$web = Get-SPWeb "https://sharepoint.company.com"
#Get Content Type and Field
$ContentType=$web.ContentTypes["Content-Type-Name"]
$FieldToAdd=$web.Fields["Field-Name"]
#Add Field to Content type
$FieldLink=New-Object Microsoft.SharePoint.SPFieldLink($FieldToAdd)
$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update()
Let’s add some error handling and make a reusable function to add a site column to content type using PowerShell!
PowerShell to Add Site Column to Content type in SharePoint
#Add SharePoint Snap-in
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Add-FieldToContentType([Microsoft.SharePoint.SPWeb] $web, [string]$ContentTypeName, [string]$SiteColumnName)
{
#Get Content Type and Field (Site column) objects
$ContentType=$web.ContentTypes[$ContentTypeName]
#Check if the content type exists
if($ContentType -ne $null)
{
#Check if the content type has the Field already
if(!$ContentType.Fields.ContainsField($SiteColumnName))
{
#Check if the site column exists
if($web.Fields.ContainsField($SiteColumnName))
{
$FieldToAdd=$web.Fields.GetField($SiteColumnName)#[$SiteColumnName]
#Add Site column to the content type
$FieldLink= New-Object Microsoft.SharePoint.SPFieldLink($FieldToAdd)
$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update($true)
Write-Host "Site Column Field Added to the Content Type!" -ForegroundColor Green
}
else
{
Write-Host "Site Column Not Found!" -ForegroundColor Red
}
}
else
{
Write-Host "Field Exists Already!" -ForegroundColor Red
}
}
else
{
Write-Host "Content type not found!" -ForegroundColor Red
}
}
Remove field from content type using PowerShell:
Here is how you can delete a site column from a content type programmatically
Function Remove-FieldFromContentType([Microsoft.SharePoint.SPWeb] $web, [string]$ContentTypeName, [string]$FieldNameToRemove)
{
#Get Content Type and Field (Site column) objects
$ContentType=$web.ContentTypes[$ContentTypeName]
#Check if the content type exists
if($ContentType -ne $null)
{
#Check if the content type has the Field
if($ContentType.Fields.ContainsField($FieldNameToRemove))
{
#Rempve the Field from the content type
$ContentType.FieldLinks.Delete($FieldNameToRemove)
$ContentType.Update($true)
Write-Host "Field removed from the Content Type!" -ForegroundColor Green
}
else
{
Write-Host "Field Doesn't Exists in the Content Type!" -ForegroundColor Red
}
}
else
{
Write-Host "Content type not found!" -ForegroundColor Red
}
}
Now, Let’s call the respective function to add or remove the site column from content type using PowerShell:
#Configuration parameters
$WebURL="https://portal.crescent.com"
$ContentTypeName="CrescentInvestments"
$FieldName="FullName" # Internal Name of the field
#Get the Web
$Web = Get-SPWeb -Identity $WebURL
#Call the method to Add field to content type
Add-FieldToContentType $Web $ContentTypeName $FieldName
#Remove-FieldFromContentType $Web $ContentTypeName $FieldName
and the result goes here:
This Programmatically Updates the given Content Type. Here is my another post on PowerShell script to add a site column to SharePoint: Create Site Column in SharePoint using PowerShell