[英]C# CAML query to sharepoint returns all items in the list (instead of only ones that mach query value)
I have the following code in my app to pull details from a sharepoint list. 我的应用程序中包含以下代码,用于从共享点列表中提取详细信息。
string siteUrl = "http://SHAREPOINTURL";
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new NetworkCredential("UN", "PW", "DOMAIN");
SP.List oList = clientContext.Web.Lists.GetByTitle("Licences");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
Console.WriteLine("Filtered List: " + collListItem.Count.ToString() + "\n");
foreach (ListItem oListItem in collListItem)
{
Console.WriteLine("Account: {0} \nLicence: {1} \nMAC: {2}\n", oListItem["Account"], oListItem["Licence"], oListItem["MAC"]);
}
In the sharepoint list I have created multiple test items but every time I run the above code all items in the list are returned regardless of what I use for the camlQuery. 在共享点列表中,我创建了多个测试项目,但是每次我运行上面的代码时,无论我为camlQuery使用什么,都将返回列表中的所有项目。
Can anyone let me know where I'm going wrong with this pretty new to C# and never touched sharepoint before this. 任何人都可以让我知道C#的这一新功能在哪里出问题,并且在此之前从未涉及过共享点。
Edit1: updated with advice from below. Edit1:更新了以下建议。
Edit2: simplified the code but still getting the same problem. Edit2:简化了代码,但仍然遇到相同的问题。
If you want to return the filtered items, in your case is returning all the items, correct? 如果您想退回已过滤的项目,那么您是否要退回所有项目,对吗? if yes, this problem is in CAMLQuery ...
如果是,此问题在CAMLQuery中。
I have read a small documentation(link below): 我已经阅读了一个小文档(下面的链接):
And I noticed the formatting ViewXml: 我注意到格式为ViewXml:
camlQuery.ViewXml = "<Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where>"
try with this code: 尝试使用以下代码:
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where></Query></View>"
I have working using with caml. 我正在使用caml。
some syntax to reflect: 一些语法来反映:
camlquery.query = "<query> ..... </query>";
camlquery.ViewXml = "<view> ..... </view>";
SORRY MY BAD ENGLISH :S 对不起我的英语不好:S
The issue is your last line of code: 问题是您的最后一行代码:
textBoxReadShow.Text = Licence + "\t\t" + MAC + "\n";
You're truncating the text in the textbox each iteration of the loop. 循环的每次迭代都将文本框中的文本截断。
You appear to want to be appending the text to the end of the textbox: 您似乎想将文本追加到文本框的末尾:
textBoxReadShow.Text += Licence + "\t\t" + MAC + "\n";
从CAML查询中删除<Query>
和</Query>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.