Is this the proper Syntax for Caml Querys? - sharepoint

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>

Related

CAML query with multiple OR conditions

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>

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>

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