问题
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
回答1:
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>";
further details you can see this artical http://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/
hops its helps..
回答2:
Try again with this - Remove the enclosed query tags from your variable camlquery
来源:https://stackoverflow.com/questions/8383616/caml-query-on-a-sharepoint-2010-list