对 SharePoint 列表的 CAML 查询,按查找字段排序
我正在尝试通过 CAML 从 SharePoint 中提取列表,并且希望返回的列表按特定字段排序。该字段是查找字段。当我将 OrderBy 设置为查找字段时,查询将无序返回,如果我使用文本字段则没问题。
当我在编辑器中构建它时,U2U CAML 查询构建器将按顺序返回此查询。
下面是我如何构建和执行查询的代码片段:
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);
其余部分是一个 for 循环,用于解析集合并显示它。如果需要的话我可以发布。
这是由 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>
不管出于什么原因,CAML 查询工具可以工作,但我的代码却不能。有人知道为什么吗?提前致谢。
编辑以反映我实际测试的代码。我有一些代码的值不正确。
I'm attempting to pull a list from SharePoint via CAML and I want the list returned ordered by a specific field. 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.
The U2U CAML query builder will return this query ordered when I build it in the editor.
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. 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.
<?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. Anyone know why? Thanks in advance.
Edited to reflect the code I'm actually testing. I had some code that had incorrect values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的代码示例与wireshark查询不匹配:
1< ;/Value>
应该是:
您不需要
元素(请参阅此处的示例)。The code sample you posted doesn't match the wireshark query:
<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 元素并删除了 LookupValue='TRUE' (元素架构中没有具有此类名称的属性)。之后一切似乎都很好。
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). After that all seems fine.