spQuery.Query = "
   <OrderBy>  <FieldRef Name='ID' Descending='True'/>   </OrderBy>
   <Where> <Eq> <FieldRef Name='Title' /> 
   <Value Type='Text'></Value> </Eq>  

What is wrong with this? It still only returns the announcements in the same order as they were added. Thanks

  • Why do you use <Where> when you only want to order it. – Anders Aune Mar 19 '12 at 18:39
  • I think he is trying to return it, but he should be using <ViewFields>. – RJ Cuthbertson Mar 19 '12 at 18:43
  • Use U2U Caml builder to test your query. It is helpful tool if your query does not work. ) – Alexander Mar 19 '12 at 18:58
  • <View><Query><OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy></Query><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>5</RowLimit></View> This works great when I test it in the U2U Query builder bu tjust refuses to work when I use it on my ascx.cs page Strange! – user7400 Mar 19 '12 at 19:51
  • 1
    try to use spQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy>" and spQuery.ViewFields="<FieldRef Name='Title'"/> – Alexander Mar 19 '12 at 20:55

According to the documentation there's no such an attribute on FieldRef as "Descending" - there is an "Ascending" one:

  Alias = "Text"
  Ascending = "TRUE" | "FALSE"
  CreateURL = "Text"
  DisplayName = "Text"
  Explicit = "TRUE" | "FALSE"    Format = "Text"
  ID = "Text"
  Key = "Text"  List = “Text”
  Name = "Text"
  RefType = "Text"
  ShowField = "Text"
  TextOnly = "TRUE" | "FALSE"
  Type = "Text">
  • So if I would to specify ascending = 'False' it would actually display items in descending order (ID #'s)? – user7400 Mar 19 '12 at 18:25
  • 2
    Yes Ascending="FALSE" is the right way to specify descending – Per Jakobsen Mar 19 '12 at 18:27
  • Well I used the same code with ascending = 'True' and it doesnt make any difference. Am I missing something? – user7400 Mar 19 '12 at 18:30
  • You're need to set Ascending = "FALSE" - not TRUE. – Alex Boev Mar 19 '12 at 18:36
  • My fault. I did set the ascending to false. Still the same result. – user7400 Mar 19 '12 at 18:46

Well, first thing is your OrderBy tags need to be contained within a Query tag. Second, I've never used Descending, just Ascending = 'False'.

Check out this example CAML query:

 <View><Query><OrderBy><FieldRef Name='Created' Ascending='True'></FieldRef>
 </OrderBy></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='Body'/>
 <FieldRef Name='Created'/></ViewFields></View>
  • SPQuery spQuery = new SPQuery(); spQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy><Where> <Eq> <FieldRef Name='Title' /> <Value Type='Text'></Value> </Eq> </Where>"; spQuery.Query = ""; spQuery.RowLimit = 5; – user7400 Mar 19 '12 at 18:32
  • Here is some context so you can take a look at what I have – user7400 Mar 19 '12 at 18:33
  • SPQuery spQuery = new SPQuery(); spQuery.Query = "<View><Query><OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy></Query><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>5</RowLimit></View>"; – RJ Cuthbertson Mar 19 '12 at 18:37
  • For one, you are clearing the query entirely by setting it to a blank string after you set it to your first query. – RJ Cuthbertson Mar 19 '12 at 18:38
  • I did remove spQuery.Query = ""; and tried using the code you provided but it still yields the same result. I know that the data is being pulled right as when I add items to my list the web part does render the changes. – user7400 Mar 19 '12 at 18:49

CAML Query in this case is not necessary. Because you can order the list with your View, check this please:

SPList list = web.Lists[listName];
SPView view = list.DefaultView;
SPListItemCollection items = list.GetItems(view);

In this code, I have DESCENDING order by ID in my list default view.


For retrieving announcement items in descending order based on ID you should only need to use OrderBy query:

 spQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.