2011-12-05 144 views
-2

林下面的代碼:SharePoint 2010列表上的CAML查詢?

string xpathresultBTADSLMAX = "BT/Max Standard"; 

if (xpathresult2 == "BT ADSL Max") 
{ 

//Creating the CAML query to perfomr the query on the list to find the required values 
SPQuery query = new SPQuery(); 

//try to find items in this list that matches the result of the XPATH query performed 
earlier 
//in this case "BT/Standard" 
string camlquery = @"<Query> 
<Where> 
<Eq> 
<FieldRef Name='Vendor_x0020_Product_x0020_Name'/> 
<Value Type='Text'>" + xpathresultBTADSLMAX + @"</Value> 
</Eq> 
</Where> 
</Query>"; 

query.Query = camlquery; 
query.ViewFields = "<FieldRef Name='Fabric_x0020_Name'/><FieldRef 
Name='Defined_x0020_For/><FieldRef name='MPLS'/>"; //selecting only the required 
fields 
from the CAML query 

SPListItemCollection listItemCollection = list.GetItems(query); 


//string fabricName = (string)item["Fabric_x0020_Name"]; commented out temporarily 

//string definedFor = (string)item["Defined_x0020_For"]; commented out temporarily 

string fabricName = (string)item["Fabric_x0020_Name"]; 
string definedFor = (string)item["Defined_x0020_For"]; 

AvailabilityCheckerResultsTwo.Controls.Add(new LiteralControl(fabricName, definedFor 
)); 

} 

在上面的代碼中,我想顯示從具有「BT /最高標準」的供應商Produc名稱列表項目的具體信息。我想要顯示的值是「Fabric Name」和「Defined For」。

然後,我希望能夠通過添加一個文字控件,但它根本不工作,以顯示他們在asp佔位符。有什麼我在這裏做錯了嗎?請提供一些關於如何實現這一目標的建議。

許多Thans提前!

更新!

因此我對代碼進行了一些更改,這些代碼基本解決了前面提到的問題。我現在可以在asp佔位符中顯示CAML查詢的結果。

string xpathresultBTADSLMAX = "BT/Max Standard"; 

//Executing the correct query based on a if condition for BT ADSL 
if (xpathresult2 == "BT ADSL Max") 
{ 

//Creating the CAML query to perfomr the query on the list to find the required values 
SPQuery query = new SPQuery(); 

//try to find items in this list that matches the result of the XPATH query performed 
earlier 
//in this case "BT/Standard" 
string camlquery = @"<Query> 
<Where> 
<Eq> 
<FieldRef Name='Vendor_x0020_Product_x0020_Name'/> 
<Value Type='Text'>" + xpathresultBTADSLMAX + @"</Value> 
</Eq> 
</Where> 
</Query>"; 

query.Query = camlquery; 
//query.ViewFields = "<FieldRef Name='Fabric_x0020_Name'/><FieldRef 
Name='Defined_x0020_For'/><FieldRef name='MPLS'/>"; //selecting only the required 
fields from the CAML query, GIVES ERROR 

SPListItemCollection listItemCollection = list.GetItems(query); 


//string fabricName = (string)item["Fabric_x0020_Name"]; commented out temporarily 

//string definedFor = (string)item["Defined_x0020_For"]; 
//string feniedFor = (string)item["Defined_x0020_For"]; 



//AvailabilityCheckerResults3.Controls.Add(new LiteralControl(fabricName + " " + 
definedFor)); 

//AvailabilityCheckerResultsTwo.Controls.Add(new LiteralControl(fabricName)); 
commented out temporarily 

//string fabricName = (string)listItemCollection["Fabric_x0020_Name"].ToString; 

/*string fabricName = listItemCollection.ToString(); 

AvailabilityCheckerResultsTwo.Controls.Add(new LiteralControl(fabricName));*/ 



foreach (SPListItem item in listItemCollection) 
{ 
try 
{ 

string results56 = (string)item["Fabric_x0020_Name"] + " " + 
(string)item["Defined_x0020_For"] + " " + "<b>MPLS:</b> " + (string)item["MPLS"] + 
"<br/>" + "<br/>"; 
AvailabilityCheckerResultsTwo.Controls.Add(new LiteralControl(results56)); 
} 
catch (Exception err) 
{ 
AvailabilityCheckerResultsTwo.Controls.Add(new LiteralControl(err.ToString())); 
} 
} 

} 

但是,我現在有另一個問題,我得到列表中的所有反饋,這不是預期的結果。下面是輸出:

羊駝 的一個骯髒的ADSL產品:CCTV第三方遠程訪問互動模式和網站測試 MPLS:沒有

地榆 家庭用戶& SOHO產品(< 5用戶網站)可以支持< 5個併發呼叫的VoIP連接。 IPT包 MPLS:是

CAML查詢應該只顯示「羊駝」的結果。我怎樣才能得到所需的項目,而不是列表中的所有項目?它可能是我做錯了!

非常感謝

回答

0

與此再次嘗試 - 從你的變量camlquery取下封閉查詢標籤

+0

我使用標籤的原因是因爲我希望查詢帶回完整的結果。然後我使用(query.ViewFields =「」;)以從結果集中選擇特定值。我遇到的問題是我不確定如何獲得顯示在asp控件中的結果 –

+0

您是否檢查過您是否獲取數據? –

+0

是的,我得到的結果! –