对 SharePoint 2010 列表的 CAML 查询? [英] CAML query on a SharePoint 2010 List?

查看:62
本文介绍了对 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".

In the above code I am trying to display specific information from a list item that has a Vendor Produc Name of "BT/Max Standard". The values I want to display are currently "Fabric Name" and "Defined For".

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

I then want to be able to display them in a asp Place holder by adding a literal control but its simply not working. Is there something that I am doing wrong here? Please provide some suggestion on how to achieve this.

提前很多!

更新!!!

所以我对代码做了一些改动,基本上解决了前面提到的问题.我现在可以在 asp 占位符中显示 CAML 查询的结果.

So I have made some changes to the code, which basically fixed the issue mentioned earlier. I am now ablke to display the results of the CAML query in the asp place holder.

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()));
}
}

} 

但是我现在有另一个问题,我从列表中获取所有记录,这不是预期的结果.这是输出:

However i now have another problem in that I am getting all reacords from the List, and this is not the intended outcome. Here is the output:

羊驼毛肮脏的 ADSL 产品用于:CCTV 第三方远程访问交互模型和网站测试MPLS:否

Alpaca A dirty ADSL product for: CCTV third-party remote access interactive models and web site testing MPLS: No

伯内特家庭用户SOHO 产品(<5 个用户站点) 可以支持 <5 个 VoIP 连接和 2 个并发呼叫.IPT包MPLS:是的

Burnet Home user & SOHO product (<5 user site) Can support <5 VoIP connections with 2 concurrent calls. IPT package MPLS: Yes

CAML 查询应该只显示Alpaca"的结果.如何仅获得所需的项目而不是列表中的所有项目?可能是我做错了什么!

The CAML query should only be displaying results for "Alpaca". How can I get only the required item and not all items in the list? Its probably something Im doing wrong!

非常感谢

推荐答案

永远不要在语法中包含 query 标签试试这个

never include query tag in syntax try this

 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/

further details you can see this artical http://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/

希望能帮上忙..

这篇关于对 SharePoint 2010 列表的 CAML 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆