我的应用程序中有以下代码,用于从共享点列表中提取详细信息。
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"]);
}
在共享点列表中,我创建了多个测试项目,但每次运行上述代码时,无论我对 camlQuery 使用什么,都会返回列表中的所有项目。
任何人都可以告诉我这个对 C# 来说是个新事物,并且在此之前从未接触过共享点,我哪里出了问题。
编辑1:根据下面的建议进行了更新。
Edit2:简化了代码,但仍然遇到同样的问题。
最佳答案
如果您想返回过滤后的项目,在您的情况下是返回所有项目,对吗?如果是,则问题出在 CAMLQuery 中...
我阅读了一份小文档(下面的链接):
我注意到 ViewXml 的格式:
camlQuery.ViewXml = "<Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where>"
尝试使用此代码:
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where></Query></View>"
我正在使用 caml。
要反射(reflect)的一些语法:
camlquery.query = "<query> ..... </query>";
camlquery.ViewXml = "<view> ..... </view>";
对不起,我的英语不好:S
关于对 sharepoint 的 C# CAML 查询返回列表中的所有项目(而不是仅匹配查询值的项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18108235/