Any script to get the list of sharepoint lists and libraries which are using content types

sns 9,226 Reputation points
2021-08-26T07:04:42.977+00:00

Do we have any PS script to get the list of SharePoint lists and libraries which are using both default content types and custom content types from SharePoint online site collection and to export to excel those data. Please help

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,793 questions
0 comments No comments
{count} votes

Accepted answer
  1. Allen Xu_MSFT 13,776 Reputation points
    2021-08-27T02:27:34.64+00:00

    Hi @sns ,

    You can find Content Type usage in a SharePoint Online site collection using below 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 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://contoso.sharepoint.com"  
    $ReportOutput ="C:\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  
    

    Don't forget to modify the variables $SiteURL and $ReportOutput before executing the script.

    The result I got on my test tenant.
    126957-image.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful