Answered by:
Order by most recent date not working in CAML Query

Question
-
Hi,
I am trying to pull items from an announcements list and I would like the results sorted by most recent first, but it is coming back as least recent first. Can someone tell me what is wrong with the query? I even tried "Modified" and I get the same result. Thanks.
SPList oList = SPContext.Current.Site.OpenWeb("ts/hse").Lists["near misses"];
SPQuery spQuery = new SPQuery();
spQuery.Query = "<Query><OrderBy><FieldRef Name='Created' Type='DateTime' IncludeTimeValue='TRUE' Ascending='False' /></OrderBy></Query>";
spQuery.RowLimit = 3;SPListItemCollection spListItemCollection = oList.GetItems(spQuery);
Sherazad
Tuesday, June 10, 2014 7:40 PM
Answers
-
Hi,
Thanks for all your input. I had to wind up calliing Microsoft about this. Here are the answers. For the query to work to find featured articles, I had to put the query in a string.concat like so and check for a boolean value of 1 not true:
SPQuery oQuery = new SPQuery();oQuery.Query = string.Concat("<OrderBy><FieldRef Name='Created' Ascending='FALSE'></FieldRef></OrderBy>",
"<Where><Eq>",
"<FieldRef Name='Featured'/>",
"<Value Type='Boolean'>1</Value>",
"</Eq></Where>");
oQuery.RowLimit = 4;Also, I had a problem with ordering by descending. The fix was that the value of the attribute "Ascending" had to be an ALL UPPERCASE "FALSE" :)
Hopefully, this will save someone else some time in the future.
Sherazad
- Marked as answer by Sherazad Thursday, June 19, 2014 2:34 PM
Thursday, June 19, 2014 2:34 PM
All replies
-
Sort date is not easy ..
Check if the query is actually right:
https://www.nothingbutsharepoint.com/sites/eusp/pages/understanding-filters-in-sharepoint-list-views.aspx
Or use the CAML Query Builder:http://www.codeproject.com/Articles/458008/CAML-Query-Builder
Tuesday, June 10, 2014 7:57 PM -
Hi,
I did a test in my environment as the capture below. It works properly.
I suggest you try the code in another list and test whether it works.
Feel free to reply with the test result.
Thanks
Patrick Liang
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com
Patrick Liang
TechNet Community Support- Proposed as answer by sk2014 Wednesday, June 11, 2014 7:02 AM
Wednesday, June 11, 2014 6:58 AM -
So I took the suggestion of downloading U2 caml query helper. The query returns the right data in the caml query helper, but when copied over into Visual Studio 2013 with update 2, the data returned is incorrect. Any other ideas :(
Sherazad
Wednesday, June 11, 2014 8:36 PM -
Thursday, June 12, 2014 4:37 AM
-
Hi,
Thanks for all your input. I had to wind up calliing Microsoft about this. Here are the answers. For the query to work to find featured articles, I had to put the query in a string.concat like so and check for a boolean value of 1 not true:
SPQuery oQuery = new SPQuery();oQuery.Query = string.Concat("<OrderBy><FieldRef Name='Created' Ascending='FALSE'></FieldRef></OrderBy>",
"<Where><Eq>",
"<FieldRef Name='Featured'/>",
"<Value Type='Boolean'>1</Value>",
"</Eq></Where>");
oQuery.RowLimit = 4;Also, I had a problem with ordering by descending. The fix was that the value of the attribute "Ascending" had to be an ALL UPPERCASE "FALSE" :)
Hopefully, this will save someone else some time in the future.
Sherazad
- Marked as answer by Sherazad Thursday, June 19, 2014 2:34 PM
Thursday, June 19, 2014 2:34 PM