SharePoint Online: Remove a Site Column from Content Type using PowerShell
Requirement: Remove a Site Column from the Content Type in SharePoint Online.
How to Remove a Column from Content Type in SharePoint Online?
In this post, we will show you how to remove a column from the content type in SharePoint Online. When you remove a site column from a content type, It does not delete it from the SharePoint Online site. It simply removes the site column’s association from the particular content type’s definition. To remove a column from the content type, follow these steps:
- Go to Site Settings >> Site Content Types (If it’s List level, Go to List Settings).
- Pick the content type from the page, In the columns section, Click on the column to remove from the content type.
- In the change content type page, click on “Remove” button and confirm the prompt to delete the column from the content type.
SharePoint Online: Remove a Column from Content Type using PowerShell
Do you need to remove a column from a content type in SharePoint Online? PowerShell makes it easy! Here is how to remove a column from the 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 Remove-ColumnFromContentType()
{
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 from web
$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 column to delete from content type
$ContentTypeFieldColl = $ContentType.Fields
$Ctx.Load($ContentTypeFieldColl)
$Ctx.ExecuteQuery()
$SiteColumn = $ContentTypeFieldColl | Where {$_.Title -eq $SiteColumnName }
if($SiteColumn -eq $null)
{
Write-host "Site Column '$SiteColumnName' doesn't exists in content Type '$ContentTypeName'" -f Yellow
}
else
{
#Get the field link from content type
$FieldLinkColl = $ContentType.FieldLinks
$Ctx.Load($FieldLinkColl)
#Remove field from content type
$FieldLink = $FieldLinkColl.GetById($SiteColumn.Id)
$FieldLink.DeleteObject()
$ContentType.Update($true)
$Ctx.ExecuteQuery()
Write-host "Site Column '$SiteColumnName' deleted from '$ContentTypeName' successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Deleting Site Column from Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Project Template"
$SiteColumnName="Project Department" #Display Name of the Field
#Call the function
Remove-ColumnFromContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName
PnP PowerShell to Remove Column from Content Type in SharePoint Online
If you want to remove a column from a content type in SharePoint Online, you can use PnP PowerShell! I will show you an example of using PowerShell to delete a column from a content type.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"
$FieldName ="Project_x0020_Manager"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the Client Context
$Context = Get-PnPContext
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName
If($ContentType)
{
#Get the Field from Content type
$Field = $ContentType.Fields.GetByInternalNameOrTitle($FieldName)
$Context.Load($Field)
$Context.ExecuteQuery()
#Remove the Field from content type
$ContentType.FieldLinks.GetById($Field.Id).DeleteObject()
$ContentType.Update($False) #Update children
$Context.ExecuteQuery()
Write-host -f Green "Field '$FieldName' has been removed from Content Type!"
}
Alternatively, you can use the Remove-PnPFieldFromContentType cmdlet in PnP PowerShell. Here is how:
Remove-PnPFieldFromContentType -Field $FieldName -ContentType $ContentTypeName