Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
spQuery.Query = "
   <OrderBy>  <FieldRef Name='ID' Descending='True'/>   </OrderBy>
   <Where> <Eq> <FieldRef Name='Title' /> 
   <Value Type='Text'></Value> </Eq>  
   </Where>";

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

share|improve this question
    
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

4 Answers 4

up vote 7 down vote accepted

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

<FieldRef
  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">
</FieldRef>
share|improve this answer
    
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
1  
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

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>";
share|improve this answer

CAML Query in this case is not necesary. 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.

Bye

share|improve this answer

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>
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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