简体   繁体   English

Sharepoint CAML中的OR问题

[英]Trouble with OR in Sharepoint CAML

I'm trying to query a list and get newsletter articles back that match the list of active newsletters. 我正在尝试查询列表并获取与活动时事通讯列表相匹配的时事通讯文章。

The trouble comes in when trying to pull items via CAML. 尝试通过CAML拉物品时遇到麻烦。 If I do an OR loop with two CONTAINS, it works great. 如果我用两个CONTAINS进行OR循环,它的效果很好。 For example: 例如:

<Where>
  <Or>
    <Contains>
      <FieldRef Name=\"Newsletter_x0020_Name\"/>
      <Value Type=\"Lookup\">April 2012</Value>
    </Contains>
    <Contains>
      <FieldRef Name=\"Newsletter_x0020_Name\"/>
      <Value Type=\"Lookup\">May 2012</Value>
    </Contains>
  </Or>
</Where>

Works great! 效果很好!

Add in a third line and we have trouble: 添加第三行我们遇到麻烦:

<Where>
  <Or>
    <Contains>
      <FieldRef Name=\"Newsletter_x0020_Name\"/>
      <Value Type=\"Lookup\">April 2012</Value>
    </Contains>
    <Contains>
      <FieldRef Name=\"Newsletter_x0020_Name\"/>
      <Value Type=\"Lookup\">May 2012</Value>
    </Contains>
    <Contains>
      <FieldRef Name=\"Newsletter_x0020_Name\"/>
      <Value Type=\"Lookup\">June 2012</Value>
    </Contains>
  </Or>
</Where>

I've made sure it wasn't the parameters inside the name column (Meaning I've tried every combination possible of April, May and June in both the two Parameter and three Parameter implementations) and nothing changes. 我已经确定它不是名称列中的参数(意思是我已经尝试了四个,五月和六月的两个参数和三个参数实现中的每个组合)并且没有任何变化。 I can use any set of parameters, and two columns always works, and three always fails. 我可以使用任何参数集,并且两列始终有效,三列始终失败。

Help? 救命?

I learned this the hard way as well - CAML groupings <And> and <Or> only work in pairs to express Boolean logic. 我也很难学到这一点--CAML分组<And><Or> 只能成对地表达布尔逻辑。

The clever bit (or annoying, depending on how you look at it) is that the groupings also work as conditions themselves. 聪明的一点(或者令人讨厌,取决于你如何看待它)是分组本身也起着条件的作用。 So in this case, it would look like this: 所以在这种情况下,它看起来像这样:

<Or>
    <Or>
        <Condition1/>
        <Condition2/>
    </Or>
    <Condition3/>
</Or>

In the case above, we are saying "Either this expression is true, or Condition3 is true"; 在上面的例子中,我们说“这个表达式是真的,或者Condition3是真的”; the first expression happens to also have a sub-expression that is evaluated and bubbled up to represent a single boolean value. 第一个表达式恰好也有一个子表达式,它被计算并冒泡以表示一个布尔值。

If you end up writing a lot of complex queries in your applications as I have, I strongly recommend hiding it behind an API or a nice object model like LINQ, because the XML is very cumbersome to write by hand, and easy to mess up. 如果你最终在你的应用程序中编写了很多复杂的查询,我强烈建议将它隐藏在API或LINQ之类的漂亮对象模型之后,因为XML手工编写非常麻烦,而且很容易搞砸。

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

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