CAML query with multiple OR conditions - sharepoint-online

I am using CAML query to retrieve a list of documents based on the approval status. Please see the query below - I read that it's not allowed to put more than two conditions in one condition group ( AND | OR). So I grouped only 2 OR conditions together - This does work but is returning irrelevant results ( it is also retrieving the documents with approval status = 'None') I have tried numerous ways but cannot get to what that I am looking for.
<Query>
<Where>
<Or>
<Or>
<eq>
<FieldRef Name="ppprovalType" />
<Value Type="Choice">Approve</Value>
</eq>
<eq>
<FieldRef Name="ApprovalType" />
<Value Type="Choice">Approve w/contingencies</Value>
</eq>
</Or>
<eq>
<FieldRef Name="ApprovalType" />
<Value Type="Choice">Change Needed</Value>
</eq>
</Or>
</Where>
</Query>
Also used the 'Neq' to list out the remaining statuses but didn't work either.
What is the right way to retrieve a document whose approval status is either '1' or '2' or '3'?

As per my test, this works for me:
<Query>
<Where>
<Or>
<Eq>
<FieldRef Name='ppprovalType' /><Value Type='Choice'>Approve</Value>
</Eq>
<Or>
<Eq>
<FieldRef Name='ApprovalType' /><Value Type='Choice'>Approve w/contingencies</Value>
</Eq>
<Eq>
<FieldRef Name='ApprovalType' /><Value Type='Choice'>Change Needed</Value>
</Eq>
</Or>
</Or>
</Where>
</Query>

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>

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.)

Is this the proper Syntax for Caml Querys?

I have a working Caml Query like so:
<View><Query><Where><Eq><FieldRef Name=\'ptli_TravelersEmail\' /><Value Type=\'Text\'>' + payeename + '</Value></Eq></Where></Query></View>
...that retrieves "records" from a Sharepoint list where the value in the 'ptli_TravelersEmail' field equates to the value of the passed-in arg "payeename".
To add another clause to the query, to retrieve "records" where the former holds true but also where the value in the 'ptli_preparer' field equates to the value of the passed-in arg "username," do I need to repeat an entire "Where.Eq.FieldRef Name.Value..Value.Eq.Where" section, like this:
<View><Query><Where><Eq><FieldRef Name=\'ptli_TravelersEmail\' /><Value Type=\'Text\'>' + payeename + '</Value></Eq></Where><Where><Eq><FieldRef Name=\'ptli_preparer\' /><Value Type=\'Text\'>' + username + '</Value></Eq></Where></Query></View>
...or is my syntax off?
I could just try it and find out, I know, but the build/run/test process in Sharepoint takes quite awhile, and I'm hoping some Caml expert knows right off the bat.
Here's the general format for a CAML query:
<View>
<Query>
<Where>
<Eq>
<FieldRef Name="Internal_Name_of_field" />
<Value Type="Text">The value to filter against</Value>
</Eq>
</Where>
</Query>
</View>
<Eq> means "Equals." You can also use comparisons like <Neq> (Not Equals), <Lt> (Less than), <Leq> (Less than or equal to), <Gt> (Greater than), <Geq> (Greater than or equal to), <Contains>, <IsNull>, and <IsNotNull>.
When you want your CAML query to have multiple conditions, you can combine two of them inside a set of <And> tags (documented here).
<Where>
<And>
<Eq>
<FieldRef Name="Internal_Name_of_field1" />
<Value Type="Text">The value to filter against</Value>
</Eq>
<Eq>
<FieldRef Name="Internal_Name_of_field2" />
<Value Type="Text">The value to filter against</Value>
</Eq>
</And>
</Where>
You can nest <And> tags inside other <And> and <Or> tags to build arbitrarily complicated queries.
<Where>
<And>
<Eq>
<FieldRef Name="Internal_Name_of_field1" />
<Value Type="Text">The value to filter against</Value>
</Eq>
<And>
<Eq>
<FieldRef Name="Internal_Name_of_field2" />
<Value Type="Text">The value to filter against</Value>
</Eq>
<Eq>
<FieldRef Name="Internal_Name_of_field3" />
<Value Type="Text">The value to filter against</Value>
</Eq>
</And>
</And>
</Where>
The exact syntax used in the <Value> element can vary depending on the type of field being compared against. Type="Text" works for single line text fields, but lookup fields, date fields, and person or group fields have different syntax.
For more advanced CAML queries, note the placement of OrderBy and RowLimit elements:
<View>
<Query>
<Where>
<Eq>
<FieldRef Name="Internal_Name_of_field" />
<Value Type="Text">The value to filter against</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name="Internal_Name_of_field" />
</OrderBy>
</Query>
<RowLimit>500</RowLimit>
</View>

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>

CAML Query not ordering properly

Can anyone help me with this CAML query? When I flip the Ascending attribute from TRUE to FALSE (have also tried True and False), it doesn't re-order the result set.
The rest of the CAML is correct, it is being generated by a tool and the appropriate results are being returned.
<Where>
<And>
<And>
<Eq>
<FieldRef Name="Branch"/>
<Value Type="Text">Camp 1</Value>
</Eq>
<Eq>
<FieldRef Name="Type"/>
<Value Type="Choice">Day</Value>
</Eq>
</And>
<Geq>
<FieldRef Name="StartDateTime"/>
<Value Type="DateTime">2009-01-05T00:00:00Z</Value>
</Geq>
</And>
<OrderBy>
<FieldRef Ascending="TRUE" Name="Title" />
</OrderBy>
</Where>
Doesn't the OrderBy have to be outside the Where clause?
<Where>
<And>
<And>
<Eq>
<FieldRef Name="Branch"/>
<Value Type="Text">Camp 1</Value>
</Eq>
<Eq>
<FieldRef Name="Type"/>
<Value Type="Choice">Day</Value>
</Eq>
</And>
<Geq>
<FieldRef Name="StartDateTime"/>
<Value Type="DateTime">2009-01-05T00:00:00Z</Value>
</Geq>
</And>
</Where>
<OrderBy>
<FieldRef Ascending="TRUE" Name="Title" />
</OrderBy>
See http://msdn.microsoft.com/en-us/library/ms442728.aspx
I agree with the answer above, the <Query> must be outside the <Where>.
Please also note that when comparing with DateTime fields, it's generally a good idea to include the IncludeTimeValue attribute:
<Geq>
<FieldRef Name="StartDateTime"/>
<Value Type="DateTime" IncludeTimeValue="FALSE">2009-01-05T00:00:00Z</Value>
</Geq>
and set it to false or true, depending on whether you want to include time values or not in your query.
I spent almost an entire week trying to get date orders to work using Client OM CamlQuery object. Finally I realised that I had to set
camlQuery.DatesInUtc = true;
It seems to me that if you are using native object model and the SPQuery object that SharePoint interprets that date as UTC by default (in our environment) as soon as you move to CamlQuery object with the ClientOM you need to set this parameter.

Resources