SPSiteDataQuery with <in> operator - sharepoint

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>

Related

CAML query...what is wrong with it?

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

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

CAML Query parameter equals string

I have the following CAML Query. What I need in it is another clause that tests for the string "0 - 9" is equal to {ToyotapediaCharacterSelector} and if not go into the existing .
Can anyone help please?
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='MyCategories' />
<Value Type='Text'>{MyCategories}</Value>
</Eq>
<BeginsWith>
<FieldRef Name='Title' />
<Value Type='Text'>{MyCharacterSelector}</Value>
</BeginsWith>
</And>
</Where>
</Query>
Using a calculated column will allow you to determine if the content in your column is numeric or text. You can then use this as a filter in your view

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