CAML查询到SharePoint列表,按查找字段排序

时间:2011-02-07 15:41:02

标签: c# 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查询工具都有效,我的代码没有。谁知道为什么?提前谢谢。

编辑反映我实际测试的代码。我有一些代码值不正确。

2 个答案:

答案 0 :(得分:7)

您发布的代码示例与wireshark查询不匹配:

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

应该是:

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

您不需要<Query></Query>元素(see here for an example)。

答案 1 :(得分:1)

我试图重现这个问题。您的查询确实没有正确排序。我进行了两项更改以使其工作 - 删除了Query元素并删除了LookupValue ='TRUE'(在元素模式中没有具有此类名称的属性)。之后一切似乎都很好。

相关问题