2012-02-07 79 views
0

我下面的代碼是基於ID用於過濾:如何編寫CAML查詢以使結果包含Column1和Column2?

camlQuery = xmlDoc.CreateElement("Query"); 

       camlQuery.InnerXml = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>"; 

如何編寫CAML查詢,以便它返回(結果包含)列1和列2?

我是CAML查詢的新手,也許無法過濾並說只有某些列應該返回?

問題是,當我運行上述查詢時,它將返回列表(42)的所有SharePoint列,而我只需要2-3。

感謝,

回答

0
SPQuery.Query

MSDN文檔提供了一個很好的例子,如何使用這兩種Query屬性和SPQuery對象的ViewFields屬性:

SPList oList = oWebsiteRoot.Lists["List_Name"]; 
SPQuery oQuery = new SPQuery(); 
oQuery.ViewFields = "<FieldRef Name='Column1'/><FieldRef Name='Column2'/>"; 
oQuery.Query = "<Where><Gt><FieldRef Name='ID'/>" + 
       "<Value Type='Number'>0</Value></Gt></Where>"; 
SPListItemCollection collListItems = oList.GetItems(oQuery); 
0

嘗試用下面的代碼:

XmlDocument xmlDoc = new System.Xml.XmlDocument(); 
    XmlElement query = xmlDoc.CreateElement("Query"); 
    XmlElement viewFields = xmlDoc.CreateElement("ViewFields"); 
    XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions"); 

    query.InnerText = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>"; 
    viewFields.InnerXml = "<FieldRef Name=\"Column1\" /><FieldRef Name=\"Column2\" />"; 
    queryOptions.InnerXml = ""; 

    try 
    { 
     XmlNode ndListItems = listService.GetListItems("List_Name", null, query, viewFields, null, queryOptions, null); 
    } 

爲引用: msdn example

相關問題