2
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?

7

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>
5
  • 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. 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
  • 2
    Are you sure it worked?:) when query is not correct, Sharepoint returns all items from a list.
    – zzeneg
    Mar 9 '15 at 2:23
  • n.b. As I'm just after laboriously finding out - there are some columns SharePoint won't sort for you - such as the Note / multiline text field May 27 '20 at 20:25
0

OK try this:

"now";

This should work

1
  • "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>now</Value></Eq></Where><OrderBy><FieldRef Name=\'Date_Of_x0020__Posted\' Ascending='False' /></OrderBy></Query>"; 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.