SharePoint 2010列表上的CAML查询?

时间:2011-12-05 10:01:13

标签: c# 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 / Max Standard”的列表项中显示特定信息。我想要显示的值目前是“Fabric Name”和“Defined For”。

然后我希望能够通过添加文字控件在asp Place holder中显示它们,但它根本不起作用。有什么我在这里做错了吗?请提供一些如何实现这一目标的建议。

许多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:否

地榆 家庭用户&amp; SOHO产品(&lt; 5用户站点)可以通过2个并发呼叫支持&lt; 5 VoIP连接。 IPT包 MPLS:是的

CAML查询应该只显示“羊驼”的结果。如何才能获得所需项目而不是列表中的所有项目?它可能是我做错了!

非常感谢

2 个答案:

答案 0 :(得分:2)

绝不在语法中包含查询标记 试试这个

 string camlquery = @"<Where><Eq><FieldRef Name='Vendor_x0020_Product_x0020_Name'/>
                      <Value Type='Text'>" + xpathresultBTADSLMAX + @"</Value>
                      </Eq>
                     </Where>";

进一步的细节,你可以看到这个艺术http://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/

跳过它的帮助..

答案 1 :(得分:0)

再试一次 - 从变量camlquery中删除随附的查询标记