Group By Content Type in SharePoint Online List View
Requirement: Set a List View Group by Content Type in SharePoint Online.
How to Set a SharePoint Online View Group by Content Type?
If you want to set a list view’s group by option based on “Content Type” there is no option from the web interface (because “Content Type” is missing from the list of columns in the drop-down!). Here is how you can group by content type.
Option #1: Edit the ListView using SharePoint Designer
Here is how you can set a group by using SharePoint Designer:
- Create a List view from SharePoint web user interface
- Edit the view page in Advanced mode using SharePoint Designer. Search for “<Query/>” and replace it with:
<Query>
<GroupBy Collapse='FALSE' GroupLimit='30' >
<FieldRef Name='ContentType' />
</GroupBy>
</Query>
This method works on SharePoint On-premises as well.
Option #2: Use PowerShell to Group by “Content Type” in SharePoint Online
Create a list view in the SharePoint Online list and use this PowerShell CSOM script to update the list view’s group by using the “ContentType” field.
#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"
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName ="Documents"
$ViewName="All Documents"
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
#Get the view to update
$View = $List.Views.GetByTitle($ViewName)
$Ctx.ExecuteQuery()
if($View -ne $NULL)
{
#Define the CAML Query to Group by Content Type
$Query= "<GroupBy Collapse='FALSE' GroupLimit='30' ><FieldRef Name='ContentType' /></GroupBy>"
#Update the View
$View.ViewQuery = $Query
$View.Update()
$Ctx.ExecuteQuery()
Write-host "View Updated Successfully!" -f Green
}
else
{
Write-host "View '$ViewName' Doesn't exist in the List!" -f Yellow
}
}
Catch {
write-host -f Red "Error Updating List View!" $_.Exception.Message
}
It is now possible to do this via the ui when viewing the list itself, still not possible in the actual view editor…
I have a slightly different problem though, trying to group by two fields with the second being ContentType. It just shows me the GUID, wondering if you get the same thing? Using “Add-PnPView”
$query = ‘