SharePoint Online: Remove a Content Type from All Lists in a Site Collection using PowerShell

Requirement: Remove Content Type from All Lists and Libraries in a SharePoint Online Site Collection

PowerShell to Remove Content Type from All Lists and Libraries in a Site Collection

In SharePoint Online, there may be times when you want to remove a content type from all the lists in a site collection. For example, maybe you have created a content type and realized later that it was not the best choice, or you want to stop users from adding items of that type to lists on the site. In this article, let’s see how to remove a content type from all lists.

This PowerShell script iterates through all lists and libraries of a given site collection, scans for the provided content type, and removes it if it’s not being used!

#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 to remove content type from a site collection
Function Remove-SPOContentType([String]$SiteURL, [String]$ContentTypeName)
{
    Try{
        Write-host -f Yellow "Processing Site:" $SiteURL
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get All Lists of the Web
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Lists)
        $Ctx.Load($ctx.Web.Webs)
        $Ctx.ExecuteQuery()
 
        #Get content types of each list from the web
        ForEach($List in $Ctx.Web.Lists)
        {
            $ContentTypes = $List.ContentTypes
            $Ctx.Load($ContentTypes)
            $Ctx.ExecuteQuery()             

            #Get each content type data
            ForEach($CType in $ContentTypes)
            {
                If($CType.Name -eq $ContentTypeName)
                {
                    Try{
                        #Remove Content Type from List
                        $CType.DeleteObject()
                        $Ctx.ExecuteQuery()
                        Write-host -f Green "Removed Content Type from List/Library:" $List.Title
                        }
                    Catch {
                    write-host -f Red "`tError Removing Content Type from '$($List.Title)' :" $_.Exception.Message
                    }
                }
            }
        }
 
         #Iterate through each subsite of the current web and call the function recursively
        foreach ($Subweb in $Ctx.web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Remove-SPOContentType $Subweb.url $ContentTypeName
        }
    }
    Catch {
    write-host -f Red "Error Removing Content Type:" $_.Exception.Message
    }
}
 
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Project Proposal"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Call the function to get the content type usage
Remove-SPOContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName

Related posts:

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published.