SP2019中SharePoint CAML查询Where子句未过滤问题求助
你的问题根源在于CamlQuery.ViewXml的格式不完整——SharePoint要求CAML查询必须包裹在<View>标签内,仅提供<Query>会导致查询逻辑失效,最终返回整个列表的所有项。
修正后的代码如下:
List dashboardList = context.Web.Lists.GetByTitle("Dashboard"); CamlQuery camlQuery = new CamlQuery(); string searchfor = "CPPMC"; // 补全<View>标签,确保查询逻辑生效 camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + searchfor + "</Value></Eq></Where></Query></View>"; ListItemCollection listItems = dashboardList.GetItems(camlQuery); context.Load(listItems); context.ExecuteQuery(); foreach (ListItem item in listItems) { Console.WriteLine("{0},{1},{2}", item.Id, item["Title"], item["Processing_x0020_Date"]); }
额外排查要点:
- 确认字段内部名称正确性:
Title是默认单行文本字段的内部名称,Processing_x0020_Date的格式也符合空格转义后的内部名称规则,无需调整。 - 匹配字段类型:
Title为单行文本,Value标签的Type='Text'对应正确;如果目标字段是其他类型(如选项、数字),需同步修改Type属性值。 - 权限验证:确保当前上下文账号拥有该列表的读取权限,以及目标字段的查看权限,避免因权限问题导致查询异常。
内容的提问来源于stack exchange,提问作者prdp13
相关产品推荐
相关产品推荐

