简体   繁体   English

使用Caml筛选共享点列表时遇到问题

[英]Trouble Filtering Sharepoint List with Caml

I'm trying to filter a SharePoint list so that only the items with the Management field, which holds a string, as "Yes" will show up, but whenever I get to the ctx.ExecuteQuery() statement, my program blows up. 我正在尝试过滤SharePoint列表,以便仅显示带有“是”的包含“字符串”的“管理”字段的项目,但是每当我到达ctx.ExecuteQuery()语句时,我的程序就会崩溃。 I believe my CAMLQuery is structured correctly, so I'm not sure if I'm simply using it wrong or if I'm missing something. 我相信我的CAMLQuery的结构正确,所以我不确定我是在错误地使用它还是缺少某些东西。 Any help would be great! 任何帮助将是巨大的! thanks! 谢谢! The code I currently have is posted below: 我目前拥有的代码发布在下面:

Web myWeb = ctx.Web;
List myList = myWeb.Lists.GetByTitle("Company Employees");
SPClient.View view = myList.DefaultView;
CamlQuery qry = new CamlQuery();
qry.ViewXml = "<Query>" + "< Where >" + "<Eq>" + "< FieldRef Name='Management'/>" + "< Value Type='Text'>Yes</ Value >" + "</Eq>" + "</ Where >" + "</ Query >";
myList.GetItems(qry);
ListItemCollection listItems = myList.GetItems(qry);
ctx.Load(listItems);
ctx.ExecuteQuery();

Your code appears to be missing the <View> tag which would wrap around your <Query> tag in the CAML. 您的代码似乎缺少<View>标记,该标记会环绕CAML中的<Query>标记。

With the addition of the <View> root element, the correct CAML XML would be as follows: 添加<View>根元素后,正确的CAML XML将如下所示:

qry.ViewXml = 
    "<View>"+
         "<Query>"+
            "<Where>"+
                 "<Eq>"+
                     "<FieldRef Name='Management'/>"+
                     "<Value Type='Text'>Yes</Value>"+
                 "</Eq>"+
            "</Where>"+
        "</Query>"+
   "</View>";

Additional Troubleshooting 其他疑难解答

To help troubleshoot, you can try running the same query through the JavaScript client object model. 为了帮助进行故障排除,您可以尝试通过JavaScript客户端对象模型运行相同的查询。

  1. Visit the SharePoint site in Internet Explorer and hit F5 to open up the developer tools. 在Internet Explorer中访问SharePoint网站,然后按F5打开开发人员工具。
  2. On the Console tab, enter the following lines of code and execute (by pressing Enter or Ctrl + Enter ) them one line at a time: 在控制台选项卡上,输入以下代码行,然后一次(通过按EnterCtrl + Enter )执行一行:

- -

var ctx = new SP.ClientContext();
var list = ctx.get_web().get_lists().getByTitle("Company Employees");
var qry = new SP.CamlQuery();
qry.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\"Management\"/><Value Type=\"Text\">Yes</Value></Eq></Where></Query></View>");
var items = list.getItems(qry);
ctx.load(items);
ctx.executeQueryAsync(function(){alert("success!");},function(sender,args){alert(args.get_message());});

POST HELP SOLUTION Thanks to your help, I was able to figure out how to create a new view with the desired filtering by using the following code. 后期帮助解决方案在您的帮助下,我能够通过以下代码找出如何使用所需的过滤器创建新视图。 The main problem was with the Caml Query--I had to remove the and tags and then delete a few of the lines before creating the view. 主要问题是与Caml查询有关的-在创建视图之前,我必须删除和标记,然后删除几行。 Below is my working solution: 以下是我的工作解决方案:

Web myWeb = ctx.Web;
List myList = myWeb.Lists.GetByTitle("Company Employees");
SPClient.View view = myList.DefaultView;
CamlQuery qry = new CamlQuery();
qry.ViewXml =
"<Where><Eq><FieldRef Name=\"Management\"/><Value Type='Text'>Yes</Value></Eq></Where>";
ViewCollection viewColl = myList.Views;
string[] viewFields = { "Title", "Promoted", "Intern", "Management" };
ViewCreationInformation creationInfo = new ViewCreationInformation();
creationInfo.Title = "Management";
creationInfo.RowLimit = 50;
creationInfo.ViewFields = viewFields;
creationInfo.ViewTypeKind = ViewType.None;
creationInfo.SetAsDefaultView = false;
creationInfo.Query = qry.ViewXml;
viewColl.Add(creationInfo);
ctx.ExecuteQuery();

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

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