SharePoint Online: Set Sort Order, Group By in a List View using PowerShell

Requirement:  Set Sort Order, Group By in a SharePoint Online List View using PowerShell.

How to Set Sort Order and Group By Settings for a SharePoint Online List View?

In this article, we’ll walk through the steps to using PowerShell to set the sort order and group by fields in a SharePoint Online list view. To set sort order or group by in SharePoint List view, modify the view and select the field in sort and group by sections.

sharepoint online list view sort order group by

PowerShell to Set Sort Order, Group By in a List View in SharePoint Online

By automating this task with PowerShell, we can save time and ensure consistency across our SharePoint sites. Here is the PowerShell to set sort and group by options for a list view.

#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 
        $Query= "<OrderBy><FieldRef Name='LinkFilenameNoMenu' /></OrderBy>"

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

Similarly, to set Group By clause, use:

#Define the CAML Query 
$Query= "<GroupBy Collapse='TRUE' GroupLimit='30' ><FieldRef Name='Author' /></GroupBy>"

To set sort order, group by settings for SharePoint On-premises List view, refer: Set Sort Order, Filter, Group By in SharePoint Views using PowerShell

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!

2 thoughts on “SharePoint Online: Set Sort Order, Group By in a List View using PowerShell

  • This works fine for a single named field like ‘Created’ but not for ‘Created By’ where it has a space in it. I even tried ‘Created_x0020_By’ and that didn’t work. Do you know how to do this where there is a space in it?

    Reply
    • Well, it uses “Internal Name” of the field! So, “Created By” field’s internal name is “Author”.

      Reply

Leave a Reply

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