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
Lets 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)
 
    #Get all content types from the web
    $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:
Lets generate a HTML report with all 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://abraaj.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
SharePoint Online: Get Content Type Inventory Report using PowerShell SharePoint Online: Get Content Type Inventory Report using PowerShell Reviewed by Salaudeen Rajack on March 28, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.