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?

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:

#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
$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)

    if($View -ne $NULL)  
        #Define the CAML Query 
        $Query= "<OrderBy><FieldRef Name='LinkFilenameNoMenu' /></OrderBy>"

        #Update the View
        $View.ViewQuery = $Query

        Write-host "View Updated Successfully!" -f Green
        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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • June 29, 2020 at 5:55 PM

    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?

    • June 29, 2020 at 8:03 PM

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


Leave a Reply