1
SPWeb _SPweb = _SPSite.OpenWeb();
SPList _SPlist = _SPweb.Lists[FL.ToString()];

SPQuery _spquery = new SPQuery();
_spquery.Query = "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>now</Value></Eq></Where><OrderBy><FieldRef Name='Date_Of_x0020__Posted' Ascending='False' /></OrderBy></Query>";//"<Query><OrderBy><FieldRef Name='Date_Of_x0020__Posted' Ascending='false' /></OrderBy></Query>";///CAMLquery  for oderby posted date

 SPListItemCollection FaqCollection = _SPlist.GetItems(_spquery);

if (FaqCollection != null || FaqCollection.Count <= 0)
{    
   foreach (SPListItem item in FaqCollection)
    {    
    }
}

Code above is retrieving the data from a list but not ordering, it was working perfectly, but it's not working any more. What have I missed here?

4

I think the problem is with including Query tag in your CAML. Just remove it.

Here is a snippet that should work:

<Where>
 <Eq>
   <FieldRef Name="Title" />
   <Value Type="Text">now</Value>
 </Eq>
</Where>
<OrderBy>
 <FieldRef Name="Date_Of_x0020__Posted" Ascending="False" />
</OrderBy>
  • YES ITS WORKING NOW,THANX,COULD YOU TELL ME WHAT HAPPENED ?IM NOT CLEAR ABOUT IT,IM NEW TO SHAREPOINT. – mzonerz Mar 8 '15 at 11:33
  • Well, basically, it's a very common mistake. You probably copied CAML from somewhere like CAML Builder or another forum. For instance, Caml Builder includes Query tag which should not be there when using server-side C# code. You only need Where and OrderBy tags. That's all there is to it. – Denis Molodtsov Mar 8 '15 at 11:57
  • THANK YOU VERY MUCH.YES I USED u2u,BUT IM CONFUSED HOW DID IT WORK BEFORE? – mzonerz Mar 8 '15 at 12:11
  • 1
    Are you sure it worked?:) when query is not correct, Sharepoint returns all items from a list. – Evgenii Vilkov Mar 9 '15 at 2:23
0

OK try this:

"now";

This should work

  • "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>now</Value></Eq></Where><OrderBy><FieldRef Name=\'Date_Of_x0020__Posted\' Ascending='False' /></OrderBy></Query>"; – Richard Przybylski Aug 15 '18 at 17:39

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.