简体   繁体   English

CAML查询到SharePoint列表返回整个集

[英]CAML Query to SharePoint list returns entire set

I've been running into an issue where if I execute a CAML query in C#, my ListItemCollection contains the entirety of the list. 我一直遇到一个问题,如果我在C#中执行CAML查询,我的ListItemCollection包含整个列表。 Here is a snippet of my scrubbed code maybe you can see what I've done wrong. 这是我擦洗代码的片段,也许你可以看到我做错了什么。 While debugging, I've found that that the XML generated is what I expected with the values read from a file. 在调试时,我发现生成的XML是我从文件中读取的值所期望的。 There seems to be an issue with actually doing the query and loading the results. 实际执行查询和加载结果似乎存在问题。 The steps I've done to do that here don't seem correct to me, I feel like I'm missing a step. 我在这里做的步骤对我来说似乎不正确,我觉得我错过了一步。

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();

In SharePoint CSOM the root element for CamlQuery.ViewXml property is <View> , for example: 在SharePoint CSOM中, CamlQuery.ViewXml属性的根元素是<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;
}

References 参考

Using the Client Object Model 使用客户端对象模型

So after a long search I've determined it was an issue with the CAML Query itself. 所以经过长时间的搜索,我已经确定这是CAML查询本身的一个问题。 Apparently I need to enclose the XML with a VIEW tag or else it doesn't even execute the query. 显然我需要用一个VIEW标签包含XML,否则它甚至不执行查询。 The following change actually works for some reason. 以下更改实际上是出于某种原因。

camlQuery.ViewXml = "<View><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></View>";

Solution source 解决方案来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM