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