对 sharepoint 的 C# CAML 查询返回列表中的所有项目(而不是仅匹配查询值的项目)

标签 c# sharepoint caml

我的应用程序中有以下代码,用于从共享点列表中提取详细信息。

            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 中...

我阅读了一份小文档(下面的链接):

Microsoft MSDN

我注意到 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/

相关文章:

c# - 我可以使用 LINQ 将两个数组的值相加吗?

c# - Windows 窗体中的切换开关控件

Sharepoint:更新内容类型时,基于内容类型的列表会如何处理?

xml - CAML 查询区分大小写的搜索

recursion - ViewAttributes 递归范围不起作用 SharePoint 2013 CAML 查询以递归方式从文档库中获取所有文件

c# - 如何查询复数n :m relationship for getting an IEnumerable in C#

sharepoint - 如何安排 Sharepoint 工作流程的执行

CAML 中的 SQL IN 等效项

SharePoint:如何使用 CAML 查询从列表中获取前 5 条记录

c# - 如何指定 .NET 泛型约束中不允许的类型?