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 占位符中显示它们,但它根本不起作用。我在这里做错了什么吗?请提供一些关于如何实现这一目标的建议。
提前很多感谢!
更新了!!!
所以我对代码做了一些修改,基本上解决了前面提到的问题。我现在可以在 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 连接和 2 个并发呼叫。 IPT套餐 MPLS:是
CAML 查询应仅显示“Alpaca”的结果。如何只获取所需的项目而不是列表中的所有项目?这可能是我做错了什么!
非常感谢
Im the following code:
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
));
}
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".
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.
Many Thans in advance!
UPDATED!!!
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:
Alpaca
A dirty ADSL product for: CCTV third-party remote access interactive models and web site testing
MPLS: No
Burnet
Home user & SOHO product (<5 user site) Can support <5 VoIP connections with 2 concurrent calls. IPT package
MPLS: Yes
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!
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
永远不要在语法中包含 query 标签
尝试
更多详细信息,您可以查看这篇文章 http ://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/
希望有帮助..
never include query tag in syntax
try this
further details you can see this artical http://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/
hops its helps..
再试一次 - 从变量 camlquery 中删除随附的查询标签
Try again with this - Remove the enclosed query tags from your variable camlquery