kwizcom banner advertisement

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
$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 
        $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
SharePoint Online: Set Sort Order, Group By in a List View using PowerShell SharePoint Online: Set Sort Order, Group By in a List View using PowerShell Reviewed by Salaudeen Rajack on January 22, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.