SharePoint Developer Center >
SharePoint Products and Technologies Forums
>
SharePoint - Development and Programming
>
Sorting SPListItemCollection using CAML Query?
Sorting SPListItemCollection using CAML Query?
- Hi,
I have a SPListItemCollection from all the list items from a list I retrieve programmatically, is there a way that I can
sort by the Title alphabetically? or sort by date descending?
Is there a way to do this? Can CAML Query do this? Thanks.
Answers
You can add multiple fieldrefs with the orderby. Although your best bet for creating CAML queries is U2U's CAML query builder (http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx)
It'll give you a nice GUI that you can mess aroudn with to get the results that you need. YOu can then cut and paste the CAML into your app.
Ch. - My Blog- Marked As Answer byff4930 Monday, November 09, 2009 6:21 PM
- Hi,
You can try the following code,
<OrderBy>
<FieldRef Name="AManager"/>
<FieldRef Name="AMOldestDate" Ascending="FALSE"/>
<FieldRef Name="BManager"/>
<FieldRef Name="BMOldestDate" Ascending="FALSE"/>
</OrderBy>
- Marked As Answer byff4930 Monday, November 09, 2009 6:21 PM
All Replies
- CAML can do this using the OrderBy element, for example:
Here's some code to show how it can be used:<Query> <OrderBy> <FieldRef Name='Title' /> </OrderBy> </Query>
SPList list= web.Lists["MyList"]; SPQuery q=new SPQuery(); q.Query="<OrderBy><FieldRef Name='Title' /></OrderBy>"; SPListItemCollection items = list.GetItems(q);
Ch. - My Blog - Refer to this link for using SPQuery to sort http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/
---
Rajesh | My Blog
MCTS - WSS AD, WSS Config, MOSS Config. - Hi,
Thanks for the replies, I have a complex query, was wondering if it is possible.
I have a list with a field named Managers and a date column. I want to first sort by Managers then by oldest date.
IE:
AManager:
//all the items related to AManager but sorted by descending date
...
..
.
BManager
//all the items related to BManager but sorted by descending date
...
..
.
You can add multiple fieldrefs with the orderby. Although your best bet for creating CAML queries is U2U's CAML query builder (http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx)
It'll give you a nice GUI that you can mess aroudn with to get the results that you need. YOu can then cut and paste the CAML into your app.
Ch. - My Blog- Marked As Answer byff4930 Monday, November 09, 2009 6:21 PM
- Hi,
You can try the following code,
<OrderBy>
<FieldRef Name="AManager"/>
<FieldRef Name="AMOldestDate" Ascending="FALSE"/>
<FieldRef Name="BManager"/>
<FieldRef Name="BMOldestDate" Ascending="FALSE"/>
</OrderBy>
- Marked As Answer byff4930 Monday, November 09, 2009 6:21 PM
- Thanks for replies!


