I am working on proof-of-concept code to dynamically generate CAML based on keywords provided to a highly-specific search web service that I am writing. I am not using the SharePoint-provided search web service for this proof. I have done so already for what I am trying to achieve. From all of my research, I cannot find a close example for what I am trying to achieve, which is to check multiple fields for multiple values. Yes, I have looked on SO already for my answer, including this one: Need help on building CAML Query.
With that said, if it is possible, how can the following SQL-like query be written in CAML?
SELECT FirstName, LastName, Description, Profile
FROM SomeFakeTable
WHERE (FirstName = 'John' OR LastName = 'John' OR Description = 'John' OR Profile='John')
AND (FirstName = 'Doe' OR LastName = 'Doe' OR Description = 'Doe' OR Profile='Doe')
AND (FirstName = '123' OR LastName = '123' OR Description = '123' OR Profile='123')
This code will dynamically generate the expression for you with the nested clauses. I have a scenario where the number of "OR" s was unknown, so I'm using the below. Usage:
And here's the code:
You can try U2U Query Builder http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx you can use their API U2U.SharePoint.CAML.Server.dll and U2U.SharePoint.CAML.Client.dll
I didn't use them but I'm sure it will help you achieving your task.
Since you are not allowed to put more than two conditions in one condition group (And | Or) you have to create an extra nested group (MSDN). The expression
A AND B AND C
looks like this:Your SQL like sample translated to CAML (hopefully with matching XML tags ;) ):