kwizcom banner advertisement

SharePoint Online: Remove Content Type From List using PowerShell CSOM

Requirement: Remove a Content type from SharePoint Online List.

How to  Remove a Content Type from List in SharePoint Online?
  • Login to your SharePoint Online site >> Navigate to the list or library settings 
  • Under "Content Types", Click on the appropriate content type name that you wish to remove from List.
  • In List Content Type page, Click on "Delete this content type" link and confirm the prompt once to remove the content type from the list
    sharepoint online powershell remove content type from list

Delete Content Type from List using PowerShell in SharePoint Online
To remove a content type from SharePoint Online list, use this PowerShell script.
#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-ContentTypeFromList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )

    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)

        #Get the content type from list
        $ContentTypeColl = $List.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()

        #Get the content type to remove
        $CTypeToRemove = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($CTypeToRemove -ne $Null)
        {
            #Remove content type from list
            $CTypeToRemove.DeleteObject()
            $Ctx.ExecuteQuery()

            Write-host "Content Type '$ContentTypeName' Removed From '$ListName'" -f Green
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow
            Return 
        }
   }
    Catch {
        write-host -f Red "Error Removing Content Type from List!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="ProjectMilestone"

#Call the function
Remove-ContentTypeFromList -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName
Please note, you can't remove content type from a list if the list has any items created based on the particular content type you wish to remove! This could result in "Content Type is still in use" error both from SharePoint UI or using PowerShell to remove content type from list.
SharePoint Online: Remove Content Type From List using PowerShell CSOM SharePoint Online: Remove Content Type From List using PowerShell CSOM Reviewed by Salaudeen Rajack on 2:16 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.