CAML request to SharePoint list, sorted by search field

I am trying to pull a list from SharePoint through CAML and I want the list to be returned by ordering a specific field. The field is a search field. The query is returned unordered when I set OrderBy as a search field if I use a text field in order.

The UAMU CAML query designer will return this request when I create it in the editor.

Here is a code snippet of how I build and execute a query:

String baseQuery = "<Query><Where><Eq><FieldRef Name='paApproved' /><Value Type='Boolean'>1</Value></Eq></Where><OrderBy><FieldRef Name='paState' Ascending='True' LookupValue='TRUE' /></OrderBy></Query>"; qStates.Query = baseQuery; SPListItemCollection byState = web.Lists["paUpdates"].GetItems(qStates); 

The rest is a for loop, which parses the collection and displays it. I can publish this if necessary.

Here's the SOAP call made by the CAML request tool, I cleaned it from the HTTP stream using wirehark.

 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"> <listName>paUpdates</listName> <query> <Query xmlns=""> <Where> <Eq> <FieldRef Name="paApproved" /> <Value Type="Boolean">1</Value> </Eq> </Where> <OrderBy> <FieldRef Name="paState" Ascending="False" /> </OrderBy> </Query> </query> <viewFields> <ViewFields xmlns="" /> </viewFields> <queryOptions> <QueryOptions xmlns="" /> </queryOptions> </GetListItems> </soap:Body> </soap:Envelope> 

For some reason, the CAML request tool works, my code does not. Does anyone know why? Thanks in advance.

Edited to reflect the code I'm actually testing. I had code that had the wrong values.

+7
source share
2 answers

Your sample code does not match wirehark request:

<Query><Where><Eq><FieldRef Name='paApproved' /><Value Type='Boolean'>1</Value></Eq></Where><OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy></Query>

Must be:

<Where><Eq><FieldRef Name="paApproved" /><Value Type="Boolean">1</Value></Eq></Where><OrderBy><FieldRef Name="paState" Ascending="False" /></OrderBy>

You do not need <Query></Query> elements ( see here for an example ).

+7
source

I tried to reproduce the problem. Your request doesnโ€™t really sort correctly. I made two changes to make it work - I deleted the Query element and deleted LookupValue = 'TRUE' (there is no attribute with that name in the element schema). After that, everything seems beautiful.

+1
source

All Articles