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:
<Where> <Or> <Or> <Or> <Or> <Eq><FieldRef Name='Author'/><Value Type='Text'>A</Value></Eq> <Eq><FieldRef Name='Author'/><Value Type='Text'>B</Value></Eq> </Or> <Eq><FieldRef Name='Author'/><Value Type='Text'>C</Value></Eq> </Or> <Eq><FieldRef Name='Author'/><Value Type='Text'>D</Value></Eq> </Or> <Eq><FieldRef Name='Author'/><Value Type='Text'>E</Value></Eq> </Or> </Where>
Thanks. This was a saver!
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: [...]
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... [...]
Does this apply for the And element as well? They each can only have 2 child nodes? This makes complex queries even more complex...
Also does the order matter? Meaning that the group of are at the front rather than a group of at the end?
That is just sad
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.
Saved me some seaching, thanks for posting this
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.
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 .....