2

I had a question earlier (that was solved) about why I was limited to two paramters in CAML.

For reference: Trouble with OR in Sharepoint CAML

What I now want to know is, can I reformat this CAML query to accept 5 OR parameters, where I collect ALL items from a list WHERE I find a match for any of the 5 parameters.

The reason I ask, is I am limited to a Designer deploy of a Sandbox solution web part. These solutions have very limited resources, and I want to have as efficient a query as possible, and most LINQ solutions don't alter the SQL calls as efficiently as CAML does.

Community
  • 1
  • 1
Wesley
  • 5,381
  • 9
  • 42
  • 65

1 Answers1

5

Yes. There is no set limit to OR's. (Due to how the CAML is turned into SQL there is a maximum depth of about 2000 chained operators, but this can be increased by balancing the query tree.)

Just keep nesting the OR's for the desired combination. I recommend using XNode/XElement and recursion to create a function to do this "magically".

Note that the nesting is very simple (I have written in in S-expression form, which is also prefix to show what happens, along with an infix C# conditional equivalent):

1: (OR a b)                => a || b                => a || b
2: (OR (OR a b) c)         => (a || b) || c         => a || b || c
3: (OR (OR (OR a b) c) d)  => ((a || b) || c) || d  => a || b || c || d

Note that each new OR just "wraps" the previous expression.

Performance is based on the performance of the underlying SQL Server database and varies based on which field is being used in the CAML. If the field is part of an index then the operations are very fast for "equals" or "range" conditions. Even if the columns are not part of an index, for "smallish" sized lists it is still a very fast operation.

Best bet is to "just try it" with a tool like the U2U CAML Query Builder ("Windows Version") and see what performance issues there are, if any.

Happy coding.