OR problem in Sharepoint CAML

I am trying to query for a list and get news articles that match the list of active newsletters.

The problem occurs when trying to pull items through 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 the third line and we have problems:

<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 made sure that these are not parameters inside the name column (which means I tried every combination in April, May and June in both two parameters and three Parameter implementations) and nothing changes. I can use any set of parameters and two columns always work and three always fail.

reference

+3


source to share


1 answer


I also understood this so diligently - CAML-groupings <And>

and <Or>

work only in pairs
to express logical logic.

The smart bit (or annoying, depending on how you look at it) is that groupings also work like the conditions themselves. So in this case it would look like this:

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

      



In the above case, we say, "Either this expression is true or Condition3 is true"; the first expression also has a subexpression that is evaluated and buffered to represent a single boolean value.

If you end up writing a lot of complex queries in your applications like I do, I highly recommend hiding it behind an API or a pretty object model like LINQ, because XML is very cumbersome to write by hand and it's easy to mess up.

+4


source







All Articles