Home
Do you know how to sort in view by a column through code
  v2.0 Posted at 25/07/2013 9:25 AM by System Account
​​​You may know that it is quite easy to sort view by a column through the UI.
SortInView.png
Figure: Change view column ​sort from web UI
But when you are trying to do that via code, you may find a pretty tricky issue.
You can use some code like:

view.Query = "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\" /></OrderBy>";

Figure: Use code to change view sort
but the below code won't work:

view.Query = "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"False\" /></OrderBy>";

Bad Example - the Ascending attribute is case-sensitive
The full code should be some code ​like:

SPView view = list.DefaultView;
view.Query = "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\" /></OrderBy>";
view.Update();​​​​

​Good Example - the Ascending attribute is using capital charactors as it is case-sensitive
                 
                    
                    

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: