SharePoint Online: Get Content Type Inventory Report using PowerShell

Requirement: SharePoint Online PowerShell to get content types report.

SharePoint Online: Get Content Types using PowerShell

Keeping track of all content types used in your SharePoint Online environment can be daunting. PowerShell can help you manage and keep track of your SharePoint Online content types by providing an inventory of all types. This blog post will show you how to get a report of all content types in SharePoint Online using PowerShell. This report can be helpful for documenting the content types that are in use in your environment.

Let’s get all content types of a SharePoint Online site using PowerShell and export them to a CSV file:

#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"

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

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #sharepoint online powershell get all content types
    $ContentTypes = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypes)
    $Ctx.ExecuteQuery()
 
    $ContentTypes | Select Name,Description,Id, Group
    #Export to CSV
    $ContentTypes | Select Name,Description,Id, Group | Export-CSV $OutputFile -notypeinformation -Delimiter "," -Append
}
Catch {
    write-host -f Red "Error Getting Content Types!" $_.Exception.Message
}

This script generates a CSV file with all content types from the given site :

sharepoint online get content type

Generate Content Types Inventory HTML Report using PowerShell:

Let’s generate an HTML report with all the above data, fields, and field types of each content type.

#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"

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

#Define CSS Styles
$HeadTag = @"
<style type="text/css">
table {
 border-collapse: collapse; font-family: verdana,arial,sans-serif;
 font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9;
 border: b1a0c7 0.5pt solid; border-spacing: 1px; border-collapse: separate; /*Sal Table format */  
}
 
th {
border-width: 1px; padding: 5px; background-color:#8064a2;
border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; 
color: white;  font-size: 11pt;  font-weight: 700;  text-decoration: none;
}
 
td {
 border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: black; 
 font-size: 11pt; font-weight: 400; text-decoration: none; 
 padding:5px;
}
 
tr:nth-child(even) { background-color: #e4dfec; }
</style>
"@
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #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 content types from the web
    $ContentTypes = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypes)
    $Ctx.ExecuteQuery()
 
    $BodyContent="<center><h3> SharePoint Online: Content Type Inventory Report </h3></center>"
    $BodyContent += $ContentTypes | Select Name,Description,Id, Group | ConvertTo-Html -Fragment

    ForEach($CType in $ContentTypes)
    {
        #Get Content type fields
        $Ctx.load($CType.Fields)
        $Ctx.ExecuteQuery()

        #Add the Data to Report
        $BodyContent += "<br/><th><b>Content Type Name: </b>$($CType.Name)</th>"
        $BodyContent += $CType.Fields | where { $_.Hidden -eq $false -and $_.Title -ne "Content Type" } | Select Title, @{Expression={$_.TypeDisplayName }; Label="Field Type"} | ConvertTo-Html -Fragment
    }
    ConvertTo-HTML -Title "Content Type Inventory Report" -Head $HeadTag -Body $BodyContent | Out-File $ReportOutput

    Write-host -f Green "Content Type Report Generated Successfully!" $_.Exception.Message
}
Catch {
    write-host -f Red "Error Generating Content Types Report!" $_.Exception.Message
}

And the sample output of the report, which lists all content types and their fields:

sharepoint online powershell get content type

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *