Trouble with OR in Sharepoint CAML

故事扮演 提交于 2019-12-23 21:32:33

问题


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. If I do an OR loop with two CONTAINS, it works great. 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?


回答1:


I learned this the hard way as well - CAML groupings <And> and <Or> only work in pairs to express Boolean logic.

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"; 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.



来源:https://stackoverflow.com/questions/9730337/trouble-with-or-in-sharepoint-caml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!