CAML Query with multiple conditions - sharepoint

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>

Related

How to write CAML query with nested ANDs or ORs

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>

CAMLQuery with multiple or conditions?

I'm trying to add another OR condition in my CAMLQuery below, but nothing seems to be working. Can someone help?
Here is the existing and working query. I'm trying to add another Or condition so that the query is, (Permissions_Type = x1 OR Permissions_Type = x2) AND (Category = y1 OR Category = y2) AND (User_Specific = z1 OR User_Specific = z2).
<Query>
<Where>
<And>
<And>
<Or>
<Eq>
<FieldRef Name="Permissions_Type"/>
<Value Type="Lookup">User</Value>
</Eq>
<Eq>
<FieldRef Name="Permissions_Type"/>
<Value Type="Lookup">Superuser</Value>
</Eq>
</Or>
<Or>
<Eq>
<FieldRef Name="Category"/>
<Value Type="Text">Survey</Value>
</Eq>
<Eq>
<FieldRef Name="Category"/>
<Value Type="Text">Notification</Value>
</Eq>
</Or>
</And>
<Geq>
<FieldRef Name="Event_End" />
<Value Type="DateTime"><Today Offset="-1" /></Value>
</Geq>
</And>
</Where>
<OrderBy><FieldRef Name="Created" Ascending="FALSE" /></OrderBy>
</Query>
And I'd like for this to be my added condition... more or less where User_Specific is null OR User_Specific = z
<Or>
<IsNull><FieldRef Name="User_Specific"/></IsNull>
<Eq>
<FieldRef Name="User_Specific"/>
<Value Type="Number">9</Value>
</Eq>
</Or>
What's helpful to remember is that every <And> and <Or> node needs exactly two child nodes.
When you've got more than two AND or OR conditions, you need to combine them using nested <And> and <Or> nodes as necessary.
When you're mapping out your logic, it might be helpful to think of AND and OR as functions that each take only two parameters:
AND(
OR(permissions=user, permissions=superuser),
AND(
OR(category=survey, category=notification),
OR(user_specific=z1, user_specific=z2)
)
)
When you translate that to CAML, you'll end up with something like this:
<Where>
<And>
<And>
<Or>
<Eq>
<FieldRef Name="Permissions_Type"/>
<Value Type="Lookup">User</Value>
</Eq>
<Eq>
<FieldRef Name="Permissions_Type"/>
<Value Type="Lookup">Superuser</Value>
</Eq>
</Or>
<And>
<Or>
<Eq>
<FieldRef Name="Category"/>
<Value Type="Text">Survey</Value>
</Eq>
<Eq>
<FieldRef Name="Category"/>
<Value Type="Text">Notification</Value>
</Eq>
</Or>
<Or>
<IsNull><FieldRef Name="User_Specific"/></IsNull>
<Eq>
<FieldRef Name="User_Specific"/>
<Value Type="Number">9</Value>
</Eq>
</Or>
</And>
</And>
<Geq>
<FieldRef Name="Event_End" />
<Value Type="DateTime"><Today Offset="-1" /></Value>
</Geq>
</And>
</Where>
(I had to tack on an extra <And> for your <Geq> condition at the end.)

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