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>

Comments (12)

J. HolopainenSeptember 2nd, 2008 at 07:20

Thanks. This was a saver!

BitterSPOctober 14th, 2008 at 07:04

Thanks for the post.

PS - CAML is sh*tty.

[...] in the foot, and I’d love to see that improved more than anything else (Well, apart from CAML, that abomination of SQL). April 15th, 2009 in Sharepoint | tags: [...]

LOVE_MOSS_NOTAugust 4th, 2009 at 23:56

Microsoft, what the heck is this? c'mon seriously.

[...] link is being shared on Twitter right now. @lukerumley, an influential author, said Or or or or or or... [...]

chris speckOctober 1st, 2009 at 17:56

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

chris speckOctober 1st, 2009 at 19:17

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

TonyOctober 27th, 2009 at 18:34

That is just sad

mstumMarch 1st, 2010 at 23:16

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.

PaulApril 6th, 2010 at 15:43

Saved me some seaching, thanks for posting this

3bikNovember 3rd, 2010 at 13:46

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.

NateMay 27th, 2011 at 14:45

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