Do you know how to sort in view by a column through code
Updated by Brady Stroud [SSW] 1 year ago. See history
123
<introEmbed
body={<>
You may know that it is quite easy to sort view by a column through the UI.
<imageEmbed
alt="Image"
size="large"
showBorder={false}
figureEmbed={{
preset: "default",
figure: 'Change view column sort from web UI',
shouldDisplay: true
}}
src="/uploads/rules/do-you-know-how-to-sort-in-view-by-a-column-through-code/SortInView.png"
/>
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>";
❌ Figure: 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();
✅ Figure: Good example - The Ascending attribute is using capital charactors as it is case-sensitive
Related rules
Need help?
SSW Consulting has over 30 years of experience developing awesome software solutions.