kwizcom banner advertisement

SharePoint Online: Find Content Type usage using PowerShell

Requirement: Find where a Content Type is used in SharePoint Online

You may have to find the content type usage in scenarios like,
  • You are getting "content type is still in use" error when trying to delete a content type.
  • You are trying to figure out where a particular content type is being used, etc.
PowerShell to Find Content Type Usage in SharePoint Online
Unlike SharePoint On-premises, We don't have SPContentTypeUsage class available in CSOM. So we are left with an option: Scan each and every list to get the content type usage!
#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 get the content type usage 
Function Find-SPOContentTypeUsage([String]$SiteURL)
{
    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
        $ContentTypeUsages=@()
        ForEach($List in $Ctx.Web.Lists)
        {
            $ContentTypes = $List.ContentTypes
            $Ctx.Load($ContentTypes)
            $Ctx.Load($List.RootFolder)
            $Ctx.ExecuteQuery()
            
            #Get List URL
            If($Ctx.Web.ServerRelativeUrl -ne "/")
            {
                $ListURL=  $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
            }
            else
            {
                $ListURL=  $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
            }
  
            #Get each content type data
            ForEach($CType in $ContentTypes)
            {
                $ContentTypeUsage = New-Object PSObject
                $ContentTypeUsage | Add-Member NoteProperty SiteURL($SiteURL)
                $ContentTypeUsage | Add-Member NoteProperty ListName($List.Title)
                $ContentTypeUsage | Add-Member NoteProperty ListURL($ListURL)
                $ContentTypeUsage | Add-Member NoteProperty ContentTypeName($CType.Name)
                $ContentTypeUsages += $ContentTypeUsage
            }
        }
        #Export the result to CSV file
        $ContentTypeUsages | Export-CSV $ReportOutput -NoTypeInformation -Append

         #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
            Find-SPOContentTypeUsage($Subweb.url)
        }
    }
    Catch {
    write-host -f Red "Error Generating Content Type Usage Report!" $_.Exception.Message
    }
}

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ReportOutput ="C:\Temp\ContentTypeUsage.csv"

#Get Credentials to connect
$Cred= Get-Credential

#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }

#Call the function to get the content type usage
Find-SPOContentTypeUsage $SiteURL
Here is the sample output of the script.

This PowerShell script gets all content types of all lists and libraries in a SharePoint Online site collection. What if you want to find where a specific content type is used?

SharePoint Online: Find Where a Specific Content Type is used 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 to get a particular content type usage
Function Get-SPOContentTypeUsage([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
        $ContentTypeUsages=@()
        ForEach($List in $Ctx.Web.Lists)
        {
            $ContentTypes = $List.ContentTypes
            $Ctx.Load($ContentTypes)
            $Ctx.Load($List.RootFolder)
            $Ctx.ExecuteQuery()
            
            #Get List URL
            If($Ctx.Web.ServerRelativeUrl -ne "/")
            {
                $ListURL=  $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
            }
            else
            {
                $ListURL=  $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
            }
  
            #Get each content type data
            ForEach($CType in $ContentTypes)
            {
                If($CType.Name -eq $ContentTypeName)
                {
                    $ContentTypeUsage = New-Object PSObject
                    $ContentTypeUsage | Add-Member NoteProperty SiteURL($SiteURL)
                    $ContentTypeUsage | Add-Member NoteProperty ListName($List.Title)
                    $ContentTypeUsage | Add-Member NoteProperty ListURL($ListURL)
                    $ContentTypeUsage | Add-Member NoteProperty ContentTypeName($CType.Name)
                    $ContentTypeUsages += $ContentTypeUsage
                    Write-host -f Green "Found the Content Type at:" $ListURL
                }
            }
        }
        #Export the result to CSV file
        $ContentTypeUsages | Export-CSV $ReportOutput -NoTypeInformation -Append

         #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
            Get-SPOContentTypeUsage $Subweb.url $ContentTypeName
        }
    }
    Catch {
    write-host -f Red "Error Generating Content Type Usage Report!" $_.Exception.Message
    }
}

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ReportOutput ="C:\Temp\ContentTypeUsage.csv"
$ContentTypeName="Business Contacts"

#Get Credentials to connect
$Cred= Get-Credential

#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }

#Call the function to get the content type usage
Get-SPOContentTypeUsage $SiteURL $ContentTypeName 
This gets you all locations of the specific content type usage.

Please note, you can not delete a content type as long as the content type is in use on any of the SharePoint site. Also, In order to remove a content type from SharePoint site, any item that uses the particular content type must be either change its content type or completely removed even from recycle bins (1st stage & 2nd stage recycle bins).

Here is my another post to find where a content type is used in SharePoint On-premises: Find Content Type Usage in SharePoint using PowerShell
SharePoint Online: Find Content Type usage using PowerShell SharePoint Online: Find Content Type usage using PowerShell Reviewed by Salaudeen Rajack on April 28, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.