CAML query...what is wrong with it? - sharepoint

I have a CAML query that keeps throwing an error.
A first chance exception of type 'System.Web.Services.Protocols.SoapException' occurred in System.Web.Services.dll
The thread 0x1574 has exited with code 0 (0x0).
Yeah so not very descriptive. I am guessing it is something to do with how I am using the "Or" element.
Here is what my query looks like:
<Where>
<And>
<IsNotNull>
<FieldRef Name='myRefID' />
</IsNotNull>
<And>
<Or>
<In>
<FieldRef Name='myRefID' />
<Values>
<Value Type='Number'>1</Value>
<Value Type='Number'>2</Value>
<Value Type='Number'>3</Value>
<Value Type='Number'>4</Value>
...
<Value Type='Number'>499</Value>
</Values>
</In>
<In>
<FieldRef Name='myRefID' />
<Values>
<Value Type='Number'>500</Value>
<Value Type='Number'>501</Value>
<Value Type='Number'>502</Value>
<Value Type='Number'>503</Value>
...
<Value Type='Number'>999</Value>
</Values>
</In>
<In>
<FieldRef Name='myRefID' />
<Values>
<Value Type='Number'>1000</Value>
<Value Type='Number'>1001</Value>
<Value Type='Number'>1002</Value>
<Value Type='Number'>1003</Value>
...
<Value Type='Number'>1111</Value>
</Values>
</In>
</Or>
</And>
</And>
</Where>
Also to note that I need to search for specific IDs hence that is why I am using so many IN clauses. The ids are in order above just for illustration purposes. In my real case example the numbers are not in order. So I can't use a query that searches between a start and ending number.

You could try this query, you don't need operator "and" and "or", remember operators in CAML works only with 2 elements.
<Where>
<And>
<IsNotNull>
<FieldRef Name='myRefID' />
</IsNotNull>
<In>
<FieldRef Name='myRefID' />
<Values>
<Value Type='Number'>1</Value>
<Value Type='Number'>2</Value>
<Value Type='Number'>3</Value>
<Value Type='Number'>4</Value>
<Value Type='Number'>499</Value>
</Values>
</In>
</And>
</Where>

You have a semantic error. Just remove this wrapper element:
<Where>
<And>
<IsNotNull>
<FieldRef Name='myRefID' />
</IsNotNull>
<And> <<================================ remove it
<Or>
<In>
<FieldRef Name='myRefID' />
<Values>
<Value Type='Number'>1</Value>
<Value Type='Number'>2</Value>
To simplify CAML query creation, use the tools:
Caml Designer - the best, in my opinion
MSDN officially recommended tools

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>

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>

SharePoint 2010 Expand recurrences CAML Query exceeding list view threshold

Trying to get event items from a list, I issue a CAML query that only returns a small number of items (about 17). When I set the List View Threshold at 10000 everything works fine, but when I set the LVT at 5000, I get a "Exceeded List View Threshold set by the administrator" error.
My CAML query is pretty simple:
<Where>
<And>
<DateRangesOverlap>
<FieldRef Name="EventDate" />
<FieldRef Name="EndDate" />
<FieldRef Name="RecurrenceID" />
<Value Type="DateTime">
<Now />
</Value>
</DateRangesOverlap>
<And>
<BeginsWith>
<FieldRef Name="Place" />
<Value Type="Text">Boston</Value>
</BeginsWith>
<Or>
<Eq>
<FieldRef Name="Status" />
<Value Type="Text">Status1</Value>
</Eq>
<Eq>
<FieldRef Name="Status" />
<Value Type="Text">Status2</Value>
</Eq>
</Or>
</And>
</And>
Can anyone explain why this may be happening? Is is because when expanding recurrences SP actually runs a separate query that result in the LVT being exceeded? Any suggestions to restructure the query would be great, but I do need to look at all occurrences of recurring events (not just the master items).
In case anyone is interested, I've managed to play around with the Query and find out how to avoid hitting the threshold.
What I did was simply change the order of the query elements so that my indexed fields were listed first and the DateRangesOverlap node was listed last. Here is the result that works:
<Where>
<And>
<BeginsWith>
<FieldRef Name="Place" />
<Value Type="Text">Boston</Value>
</BeginsWith>
<And>
<Or>
<Eq>
<FieldRef Name="Status" />
<Value Type="Text">Status1</Value>
</Eq>
<Eq>
<FieldRef Name="Status" />
<Value Type="Text">Status2</Value>
</Eq>
</Or>
<DateRangesOverlap>
<FieldRef Name="EventDate" />
<FieldRef Name="EndDate" />
<FieldRef Name="RecurrenceID" />
<Value Type="DateTime">
<Now />
</Value>
</DateRangesOverlap>
</And>
</And>
</Where>

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