林下面的代码: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查询应该只显示“羊驼”的结果。我怎样才能得到所需的项目,而不是列表中的所有项目?它可能是我做错了!
非常感谢
我使用标签的原因是因为我希望查询带回完整的结果。然后我使用(query.ViewFields =“ ”;)以从结果集中选择特定值。我遇到的问题是我不确定如何获得显示在asp控件中的结果 –
您是否检查过您是否获取数据? –
是的,我得到的结果! –