我遇到了一个问题,如果我在 C# 中执行 CAML 查询,我的 ListItemCollection 包含整个列表。这是一个片段 我清理过的代码也许你可以看到我做错了什么。 在调试时,我发现生成的 XML 是我期望从文件中读取的值。似乎有一个问题 实际执行查询并加载结果。我在这里完成的步骤对我来说似乎不正确,我觉得我错过了一步。
using Microsoft.SharePoint.Client;
...
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(user, password, domain);
ClientContext clientContext = new ClientContext(uri);
clientContext.Credentials = credentials;
List list = clientContext.Web.Lists.GetByTitle(listName);
//read line of input from file and save to string[]
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<Query><Where><And><Eq><FieldRef Name=\"Entity\" /><Value Type=\"Text\">" + columns[2].Trim() + "</Value></Eq><And><Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">" + columns[0].Trim() + "</Value></Eq><Eq><FieldRef Name=\"Section\" /><Value Type=\"Text\">" + columns[1].Trim() + "</Value></Eq></And></And></Where></Query>";
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
最佳答案
在 SharePoint CSOM 中,CamlQuery.ViewXml property 的根元素是<View>
,例如:
public CamlQuery CreateInventoryQuery(string searchSku)
{
var qry = new CamlQuery();
qry.ViewXml =
@"<View>
<Query>
<Where>
<BeginsWith>
<FieldRef Name='SKU' />
<Value Type='Text'>" + searchSku + @"</Value>
</BeginsWith>
</Where>
</Query>
</View>";
return qry;
}
引用资料
关于c# - 对 SharePoint 列表的 CAML 查询返回整个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847269/