簡體   English   中英

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

[英]CAML query to a SharePoint list, order by a lookup field

我正在嘗試通過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查詢工具都能正常工作,我的代碼卻沒有。 誰知道為什么? 提前致謝。

編輯反映我實際測試的代碼。 我有一些代碼值不正確。

您發布的代碼示例與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>元素( 有關示例,請參見此處 )。

我試圖重現這個問題。 您的查詢確實沒有正確排序。 我進行了兩項更改以使其工作 - 刪除了Query元素並刪除了LookupValue ='TRUE'(在元素模式中沒有具有此類名稱的屬性)。 之后一切似乎都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM