How to write CAML query with nested ANDs or ORs - sharepoint

Before you start suggesting U2U CAML Editor or CAML Designer 2013, I already tried both, none of them allow me in an intuitevely way to group ANDS/ORS statements to do the proper query.
I have a list if Bill cycles with some fields, In SQL This would be extremely easy:
WHERE (Content Type='Bill Cycle')
AND (Status != 'Completed' ANDStatus!='Terminated' and Status!=NULL)
AND (JobAdvisor:xyz OR JobPartner:xyz or JobManager:xyz or Reviewer:xyz or AnotherUserField:xyz)
My code is more or less like this: but I am missing to group the 2 big statements here
<And>
<And>
<And>
<Neq>
<FieldRef Name='Bill_x0020_Preparation_x0020_Status' />
<Value Type='Text'>Completed</Value>
</Neq>
<Neq>
<FieldRef Name='Bill_x0020_Preparation_x0020_Status' />
<Value Type='Text'>Terminated</Value>
</Neq>
</And>
<IsNull>
<FieldRef Name='Bill_x0020_Preparation_x0020_Status' />
</IsNull>
</And>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Computed'>Bill Cycle</Value>
</Eq>
</And>
<Or>
<Or>
<Or>
<Or>
<Contains>
<FieldRef Name='Billing_x0020_Advisor_x0020_Reviewers' />
<Value Type='User'>a</Value>
</Contains>
<Contains>
<FieldRef Name='Final_x0020_P_x002F_D_x0020_Approver' />
<Value Type='User'>a</Value>
</Contains>
</Or>
<Contains>
<FieldRef Name='BillManager' />
<Value Type='User'>a</Value>
</Contains>
</Or>
</Or>
<Contains>
<FieldRef Name='Bill_x0020_Preparer' />
<Value Type='User'>a</Value>
</Contains>
</Or>

Just following your SQL example... the outer and's three things: the Bill Cycle test, the and'ed status test, and the OR block.
<And>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Computed'>Bill Cycle</Value>
</Eq>
<And>
<Neq>
<FieldRef Name='Bill_x0020_Preparation_x0020_Status' />
<Value Type='Text'>Completed</Value>
</Neq>
<Neq>
<FieldRef Name='Bill_x0020_Preparation_x0020_Status' />
<Value Type='Text'>Terminated</Value>
</Neq>
<IsNull>
<FieldRef Name='Bill_x0020_Preparation_x0020_Status' />
</IsNull>
</And>
<Or>
<Or>
<Or>
<Or>
<Contains>
<FieldRef Name='Billing_x0020_Advisor_x0020_Reviewers' />
<Value Type='User'>a</Value>
</Contains>
<Contains>
<FieldRef Name='Final_x0020_P_x002F_D_x0020_Approver' />
<Value Type='User'>a</Value>
</Contains>
</Or>
<Contains>
<FieldRef Name='BillManager' />
<Value Type='User'>a</Value>
</Contains>
</Or>
</Or>
<Contains>
<FieldRef Name='Bill_x0020_Preparer' />
<Value Type='User'>a</Value>
</Contains>
</Or>
</And>

Related

CAML Query with multiple conditions

I'm trying to add another OR condition in my CAML Query below, but nothing seems to be working. Can someone help? Please see attached picture of the library. I am trying to get the highlighted data. Here is the CAML query. Library Name and Level stays the same. I need to grab the 1st row, 3rd and 4th row. The first attempt I am trying to get the exact person (Marshall, Kim), then I am saying give me data where Line=Pure and Any Local is Yes (not working but it should give me Sandoz, Newman), then also give me where Any Line is yes and Any Local is Yes for Packaging with Level A. (Howey, Laurel).
<Query>
<ViewFields>
<FieldRef Name="AssignedTo" />
</ViewFields>
<Where>
<And>
<Eq>
<FieldRef Name='Library' />
<Value Type='Lookup'>Packaging</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='Level' />
<Value Type='Choice'>A</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='Line' />
<Value Type='Lookup'>PURE</Value>
</Eq>
<Eq>
<FieldRef Name='Place' />
<Value Type='Lookup'>Vincente, New Port, CA</Value>
</Eq>
</And>
</And>
</And>
<And>
<Or>
<And>
<And>
<And>
<Eq>
<FieldRef Name='Library' />
<Value Type='Lookup'>Packaging</Value>
</Eq>
<Eq>
<FieldRef Name='Level' />
<Value Type='Choice'>A</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='Line' />
<Value Type='Lookup'>PURE</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='AnyPlace' />
<Value Type='Boolean'>1</Value>
</Eq>
</And>
</Or>
</And>
</Where>
</Query>
I got some help from Gaurav. https://www.gaurravs.com/post/understanding-dynamic-queries
Here is the final CAML that helped.
<Query>
<ViewFields>
<FieldRef Name="AssignedTo" />
</ViewFields>
<Where>
<And>
<Eq>
<FieldRef Name='Library' />
<Value Type='Lookup'>Packaging</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='Level' />
<Value Type='Choice'>A</Value>
</Eq>
<Or>
<Eq>
<FieldRef Name='Line' />
<Value Type='Lookup'>PURE</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='AnyLine' />
<Value Type='Boolean'>1</Value>
</Eq>
<Eq>
<FieldRef Name='AnyLocal' />
<Value Type='Boolean'>1</Value>
</Eq>
</And>
</Or>
</And>
</And>
</Where>

CAML query with 4 conditions will raise this error "Cannot complete this action.\n\nPlease try again."

I have the following CAML query:-
<View Scope=\"RecursiveAll\"><Query><Where>
<And>
<Contains><FieldRef Name =\"ProjectStage1\" /><Value Type = \"Choice\">closed</Value></Contains>
<Eq><FieldRef Name =\"ProjectPriority1\" /><Value Type = \"Choice\">(1) High</Value></Eq>
<And>
<Leq><FieldRef Name=\"Modified\" /><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today OffsetDays=\"-7\"/></Value></Leq>
<Geq><FieldRef Name=\"Modified\" /><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today OffsetDays=\"-14\"/></Value></Geq>
</And></And>
</Where></Query></View>
But i am getting this error :-
Cannot complete this action.\n\nPlease try again.
can anyone advice?
I am not sure but I think you are missing one <And> tag in the query.
Please make sure it fallows the rules defined here MSDN - AND.
I didn't have possibility to double check it but maybe something like this should work:
<View Scope=\"RecursiveAll\">
<Query>
<Where>
<And>
<Contains>
<FieldRef Name =\"ProjectStage1\" />
<Value Type = \"Choice\">closed</Value>
</Contains>
<And>
<Eq>
<FieldRef Name =\"ProjectPriority1\" />
<Value Type = \"Choice\">(1) High</Value>
</Eq>
<And>
<Leq>
<FieldRef Name=\"Modified\" />
<Value Type=\"DateTime\" IncludeTimeValue=\"False\">
<Today OffsetDays=\"-7\"/>
</Value>
</Leq>
<Geq>
<FieldRef Name=\"Modified\" />
<Value Type=\"DateTime\" IncludeTimeValue=\"False\">
<Today OffsetDays=\"-14\"/>
</Value>
</Geq>
</And>
</And>
</And>
</Where>
</Query>
</View>
Basically we should fallow these kind of rules when multiple And conditions:
1. Single Condition
<Where>
<Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
</Where>
2. Two Condtion
<Where>
<And>
<Eq><FieldRef Name=’Title’ /> <Value Type=’Text’>Mr</Value></Eq>
<Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
</And>
</Where>
3. Three Condtion
<Where>
<And>
<And>
<Eq><FieldRef Name=’Title’ /> <Value Type=’Text’>Mr</Value></Eq>
<Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
</And>
<Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
</And>
</Where>
4. Four Condtion
<Where>
<And>
<And>
<Eq><FieldRef Name=’Title’ /> <Value Type=’Text’>Mr</Value></Eq>
<Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
</And>
<And>
<Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
<Eq><FieldRef Name=’Country’ /> <Value Type=’Text’>India</Value></Eq>
</And>
</And>
</Where>

AND OR AND CAML query in SharePoint

Can't figure out a good way to do this. I have a view in SharePoint that I want to filter using a query like (A and B) or (A and C). I'm trying to write this in CAML in Sharepoint Designer but am getting nowhere. This is my first time using CAML so that's not helping. Here's what I've come up with so far:
<Where>
<And>
<Or>
<And>
<Eq>
<FieldRef Name="Component" />
<Value Type="Text">ComponentX</Value>
</Eq>
<Eq>
<FieldRef Name="Review_x0020_Canceled" />
<Value Type="Boolean">0</Value>
</Eq>
</And>
</Or>
<Eq>
<FieldRef Name="Component" />
<Value Type="Text">ComponentX</Value>
</Eq>
<IsNull>
<FieldRef Name="Actual_x0020_Finish_x0020_Date" />
</IsNull>
</And>
</Where>
I'd like this to display all records where (Component=ComponentX AND Review Canceled=No) or (Component=ComponentX AND Actual Finish Date=Null)
Any ideas?
try this:
<Where>
<Or>
<And>
<Eq>
<FieldRef Name='Component' />
<Value Type='Text'>ComponentX</Value>
</Eq>
<Eq>
<FieldRef Name='Review_x0020_Canceled' />
<Value Type='Boolean'>0</Value>
</Eq>
</And>
<And>
<Eq>
<FieldRef Name='Component' />
<Value Type='Text'>ComponentX</Value>
</Eq>
<IsNull>
<FieldRef Name="Actual_x0020_Finish_x0020_Date" />
</IsNull>
</And>
</Or>
</Where>
new code caml:
<Where>
<And>
<And>
<Eq>
<FieldRef Name='Review_x0020_Canceled' />
<Value Type='Boolean'>0</Value>
</Eq>
<Eq>
<FieldRef Name='Component' />
<Value Type='Text'>ComponentX</Value>
</Eq>
</And>
<IsNull>
<FieldRef Name='Actual_x0020_Finish_x0020_Date' />
</IsNull>
</And>
</Where>
Your logic
(Component = ComponentX AND Review Canceled = NO) OR
(Component = ComponentX AND Actual Finish Date = NULL)
is equivalent to
Component = ComponentX And (Review Canceled = NO OR Actual Finish Date = NULL)
which would be this CAML query:
<Where>
<And>
<Eq>
<FieldRef Name="Component" />
<Value Type="Text">ComponentX</Value>
</Eq>
<Or>
<Eq>
<FieldRef Name="Review_x0020_Canceled" />
<Value Type="Boolean">0</Value>
</Eq>
<IsNull>
<FieldRef Name="Actual_x0020_Finish_x0020_Date" />
</IsNull>
</Or>
</And>
</Where>

CAML query throwing SoapServerException

This is the exception:
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
Here's the CAML code:
<Query>
<Where>
<And>
<And>
<And>
<And>
<And>
<And>
<And>
<Contains>
<FieldRef Name="Title">
<Value Type="Text">password</Value>
</FieldRef>
</Contains>
<Contains>
<FieldRef Name="Answer">
<Value Type="Note">pass</Value>
</FieldRef>
</Contains>
</And>
<Eq>
<FieldRef Name="PubDestination1">
<Value Type="Text">PCOM</Value>
</FieldRef>
</Eq>
</And>
<Eq>
<FieldRef Name="PubDestination2">
<Value Type="Text">2</Value>
</FieldRef>
</Eq>
</And>
<Eq>
<FieldRef Name="FaqCategory">
<Value Type="Text">Autenticação</Value>
</FieldRef>
</Eq>
</And>
<Eq>
<FieldRef Name="PublicComponent">
<Value Type="Boolean">1</Value>
</FieldRef>
</Eq>
</And>
<Geq>
<FieldRef Name="PubStartDate">
<Value Type="DateTime" IncludeTimeValue="TRUE">2011-02-07T00:00:00Z</Value>
</FieldRef>
</Geq>
</And>
<Leq>
<FieldRef Name="PubStartDate">
<Value Type="DateTime" IncludeTimeValue="TRUE">2011-02-25T00:00:00Z</Value>
</FieldRef>
</Leq>
</And>
</Where>
</Query>
The code works when I don't specify any filters...
Value element should not be inside FieldRef element. You should put both FieldRef and Value into one parent element (like Contains):
<Contains>
<FieldRef Name="Title"/>
<Value Type="Text">password</Value>
</Contains>

SharePoint CAML query for following condition

Can any one tell me what would me the CAML query for the following condition
[[Col1=22 And Col2=23] OR [Col3=Yes] ] And [ [Col4=16] OR [Col5=56 ] ]
Where Col1,Col2,Col3,Col4,Col5 are the columns of my list and 22,23,Yes 16 And 56 are some mock values.
Thanks in advance!
Sachin
This should work. Basically, you have to start writing the query with the AND outside the parenthesis and work your way into the groupings.
<Where>
<And>
<Or>
<And>
<Eq>
<FieldRef Name='Col1' />
<Value Type='Text'>22</Value>
</Eq>
<Eq>
<FieldRef Name='Col2' />
<Value Type='Text'>23</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='Col3' />
<Value Type='Boolean'>1</Value>
</Eq>
</Or>
<Or>
<Eq>
<FieldRef Name='Col4' />
<Value Type='Text'>16</Value>
</Eq>
<Eq>
<FieldRef Name='Col5' />
<Value Type='Text'>56</Value>
</Eq>
</Or>
</And>
</Where>

Resources