If you have a requirement to add a field to content type or remove a field from existing content type, use this PowerShell script:
PowerShell script to Add a field to content type:
#Get Web Object
$web = Get-SPWeb "http://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()
Lets add some error handling and make a reusable function to add 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, Lets call the respective function to add or remove site column from content type using PowerShell:
#Configuration parametersand the result goes here:
$WebURL="http://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
This Programmatically Updates the given Content Type. Here is my another post on PowerShell script to add site column to SharePoint: Create Site Column in SharePoint using PowerShell