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