CAML Or-Queries

Just another “RTFM”-Problem that I encountered the hard way:

<Or>-Queries in CAML look like this: ((((A or B) or C) or D) or E).
I accidentially assumed that they were (((A or B) or C or D) or E), but that just leads to Sharepoint 2007 giving the overly helpful “Cannot complete this action” Error message.

Example of a correct Query to have 5 OR-Directives:

          <Eq><FieldRef Name='Author'/><Value Type='Text'>A</Value></Eq>
          <Eq><FieldRef Name='Author'/><Value Type='Text'>B</Value></Eq>
        <Eq><FieldRef Name='Author'/><Value Type='Text'>C</Value></Eq>
      <Eq><FieldRef Name='Author'/><Value Type='Text'>D</Value></Eq>
    <Eq><FieldRef Name='Author'/><Value Type='Text'>E</Value></Eq>

12 thoughts on “CAML Or-Queries

  1. Does this apply for the And element as well? They each can only have 2 child nodes? This makes complex queries even more complex…

  2. Also does the order matter? Meaning that the group of are at the front rather than a group of at the end?

  3. Yes, the And element also can only have 2 child elements. Order of these two elements does not matter, but of course if you nest Or and And then Order might matter.

  4. Or you can have something like: (((A or B) or (C or D)) or E), etc.
    Same thing with And.
    Always join only two expressions.

  5. Worth mentioning, my code would not work until I grouped TWO fieldrefs in the first OR. I continued to receive the following error until adding the second fieldref: ‘One or more field types are not installed properly. Go to the list settings page to delete these fields.’ I had no CAML errors after adding it, so the error HAD NOTHING TO DO WITH the field naming.

    fu2, CAML …..