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 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 List View 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>
sharepoint online view group by content type
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 SharePoint Online list and use this PowerShell CSOM script to update the list view's group by using "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://crescenttech.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
}
There is an another trick on modern lists: Append "?groupBy=ContentType" to the View URL and hit Enter in the browser, then use "Save View as" to create a new view with group by content type!

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.