Set Sorting, Filter, Group By in SharePoint Views Programmatically
code to set the View Filter, Sort options:
//Get the List, say "Project Tasks"
SPList ProjectTasks = web.Lists["Project Tasks"]; /
/Get the view
SPView ProjectTasksView = ProjectTasks.DefaultView;
// Or you can get a specific view by: List.Views["View-Name"]
//Set the CAML Query
string viewQuery = @" <Where><Eq><FieldRef Name='Status' /><Value Type='Choice'>In Progress</Value></Eq></Where><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy> ";
//Update the view
ProjectTasksView.Query = viewQuery;
ProjectTasksView.Update();
//Update the List
ProjectTasks.Update();
U2U doesn't offer Group by in its UI, However just append <GroupBy Collapse="TRUE"><FieldRef Name="Country" /></GroupBy>to the Query to get the Group by applied.
PowerShell way to Set View Options:
$web=Get-SPWeb -identity "http://sharepoint.com" $list=$web.lists["Tasks"] $view = $list.DefaultView # To fetch a particular view, use: $list.Views | ?{$_.title -eq "View-Name"} $view.Query = '<OrderBy><FieldRef Name="Modified" Ascending="False" /></OrderBy>' $view.Update() $list.Update()