SharePoint 2010 Expand recurrences CAML Query exceeding list view threshold - sharepoint

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>

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>

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>

SPSiteDataQuery with <in> operator

First of all... the query works. With SPQuery it does. The query yields exactly one result (as expected!).
However as soon as I use SPSiteDataQuery (and I need to) it yields two results.
I realized if I don't use but instead (works in cases where there is only ONE value (such as in the example below) it works and only returns one result.
I am using the operator as suggested by this Microsoft article on querying taxonomy fields with CAML: http://msdn.microsoft.com/en-us/library/ff625182(v=office.14).aspx
If two records are returned the first contains the note field (as desired) and the second one doesn't. All other columns are identical.
So I went forth and omitted that note field from the viewfields selection - ét voilà: 1 result (unfortunately I really need that note field's content!).
<Where>
<And>
<Eq>
<FieldRef ID='c042a256-787d-4a6f-8a8a-cf6ab767f12d' /> <!-- content type name -->
<Value Type='Text'>Standard Teaser</Value>
</Eq>
<And>
<And>
<Or>
<Gt>
<FieldRef ID='51d39414-03dc-4bd0-b777-d3e20cb350f7' /> <!-- publishing exp -->
<Value Type='DateTime'>
<Today/>
</Value>
</Gt>
<IsNull>
<FieldRef ID='51d39414-03dc-4bd0-b777-d3e20cb350f7' />
</IsNull>
</Or>
<Or>
<Leq>
<FieldRef ID='a990e64f-faa3-49c1-aafa-885fda79de62' /> <!-- publishing start -->
<Value Type='DateTime'>
<Today />
</Value>
</Leq>
<IsNull>
<FieldRef ID='a990e64f-faa3-49c1-aafa-885fda79de62' />
</IsNull>
</Or>
</And>
<In>
<!-- taxonomy field -->
<FieldRef ID='CAFEF21A-D977-44F8-96E4-6F6DA6F90A59' LookupId='TRUE' />
<Values>
<Value Type='Integer'>2</Value>
</Values>
</In>
</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 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