[英]CAML query to a SharePoint list, order by a lookup field
I'm attempting to pull a list from SharePoint via CAML and I want the list returned ordered by a specific field. 我正在尝试通过CAML从SharePoint中提取列表,我希望按特定字段排序返回的列表。 The field is a lookup field. 该字段是查找字段。 The query comes back unordered when I set the OrderBy to be the lookup field, if I use a text field it's fine. 当我将OrderBy设置为查找字段时,查询返回无序,如果我使用文本字段就可以了。
The U2U CAML query builder will return this query ordered when I build it in the editor. 当我在编辑器中构建它时,U2U CAML查询构建器将返回有序的查询。
Here's a code snippet of how I build and execute the 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 that parses the collection and displays it. 其余的是一个for循环,它解析集合并显示它。 I can post that if necessary. 如有必要,我可以发布。
Here's the SOAP call made by the CAML query tool, I scraped it from the HTTP stream with wireshark. 这是CAML查询工具发出的SOAP调用,我使用wireshark从HTTP流中删除它。
<?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 whatever reason the CAML query tool works, my code doesn't. 无论出于何种原因,CAML查询工具都能正常工作,我的代码却没有。 Anyone know why? 谁知道为什么? Thanks in advance. 提前致谢。
Edited to reflect the code I'm actually testing. 编辑反映我实际测试的代码。 I had some code that had incorrect values. 我有一些代码值不正确。
The code sample you posted doesn't match the wireshark query: 您发布的代码示例与wireshark查询不匹配:
<Query><Where><Eq><FieldRef Name='paApproved' /><Value Type='Boolean'>1</Value></Eq></Where><OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy></Query>
Should be: 应该:
<Where><Eq><FieldRef Name="paApproved" /><Value Type="Boolean">1</Value></Eq></Where><OrderBy><FieldRef Name="paState" Ascending="False" /></OrderBy>
You don't need the <Query></Query>
elements ( see here for an example ). 您不需要<Query></Query>
元素( 有关示例,请参见此处 )。
I tried to reproduce the problem. 我试图重现这个问题。 Your query really doesn't sort correctly. 您的查询确实没有正确排序。 I've made two changes to make it work - removed Query element and removed LookupValue='TRUE' (there is no attribute with such name in element schema). 我进行了两项更改以使其工作 - 删除了Query元素并删除了LookupValue ='TRUE'(在元素模式中没有具有此类名称的属性)。 After that all seems fine. 之后一切似乎都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.