c#
  • sharepoint
  • caml
  • 2011-02-07 127 views 7 likes 
    7

    我試圖通過CAML從SharePoint拉清單,我想列表中返回一個特定的字段排序。該字段是查找字段。當我將OrderBy設置爲查找字段時,查詢返回無序,如果我使用文本字段就沒問題。當我在編輯器中建立它CAML查詢SharePoint列表,以便通過查找字段

    的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查詢工具的工作原理,我的代碼不會。有人知道爲什麼提前致謝。

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

    回答

    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)。

    +0

    你說得對。對不起,我正在測試一些東西。我使用「標題」來查看它是否會在文本字段上排序,而不是查找。我使用的代碼確實有「paState」作爲字段,並且不起作用。我剪下並粘貼了錯誤的版本。它將排序在文本字段上,但現在在查找字段上。 – kevingreen 2011-02-07 16:08:51

    1

    我試圖重現該問題。您的查詢確實無法正確排序。我做了兩項更改,使其工作 - 刪除了查詢元素並刪除了LookupValue ='TRUE'(元素模式中沒有這種名稱的屬性)。之後,一切似乎都很好。

    相關問題