CAML Query with IN + AND or other - sharepoint-2010

My question is about caml query SP2010,
i've got colums as follow: Country, Role, Species, Species, Topic, Documenttype, Searchtext.
There is possible that user will search by Countries (multiple->UK,France,Belgium) and type document name like Doc_num_one.txt and add other criteria like Role, Species, Species, Topic, Documenttype. I've tried different queries but I think I stucked.
Below what I tried so far:
<Query>
<Where>
<In>
<FieldRef Name="Country"/>
<Values>
<Value Type="Lookup">United Kingdom</Value>
<Value Type="Lookup">Poland</Value>
<Value Type="Lookup">Belgium</Value>
</Values>
</In>
</Where>
</Query>
or
<Query>
<Where>
<And>
<Eq>
<FieldRef Name="Title" />
<Value Type="Text">doc num two</Value>
</Eq>
<Or>
<And>
<Eq>
<FieldRef Name="Country" />
<Value Type="Lookup">United Kingdom</Value>
</Eq>
<Eq>
<FieldRef Name="Role" />
<Value Type="Lookup">Super Admin</Value>
</Eq>
</And>
<And>
<Eq>
<FieldRef Name="Country" />
<Value Type="Lookup">Belgium</Value>
</Eq>
<Eq>
<FieldRef Name="Role" />
<Value Type="Lookup">Super Admin</Value>
</Eq>
</And>
<And>
<Eq>
<FieldRef Name="Country" />
<Value Type="Lookup">Poland</Value>
</Eq>
<Eq>
<FieldRef Name="Role" />
<Value Type="Lookup">Super Admin</Value>
</Eq>
</And>
</Or>
</And>
</Where>
</Query>
Solution:
<Query>
<Where>
<And>
<In>
<FieldRef Name="Country"/>
<Values>
<Value Type="Lookup">United Kingdom</Value>
<Value Type="Lookup">Poland</Value>
<Value Type="Lookup">Belgium</Value>
</Values>
</In>
<In>
<FieldRef Name="Role"/>
<Values>
<Value Type="Lookup">Super Admin</Value>
<Value Type="Lookup">Admin</Value>
<Value Type="Lookup">User</Value>
</Values>
</In>
</And>
</Where>
</Query>

The second query does not work because it has more than two child elements within the <Or> element. If you want to test more sub-conditions, you have to introduce helper elements:
<Or>
<And>...</And>
<And>...</And>
<And>...</And>
<And>...</And>
<And>...</And>
</Or>
becomes
<Or>
<And>...</And>
<Or>
<And>...</And>
<Or>
<And>...</And>
<Or>
<And>...</And>
<And>...</And>
</Or>
</Or>
</Or>
</Or>
The same applies to <And> elements.

Related

Multiple Inner Joins CAML query

I'm trying to convert the following SQL statement to a CAML query:
SELECT t.Id, t.Name, t.CustomerId
FROM Ticket AS t
INNER JOIN
Customer AS c1 ON t.CustomerEMail = c1.EMail
INNER JOIN
Customer AS c2 ON c1.CompanyNo = c2.CompanyNo
WHERE (c2.Email = 'client1#co1.com')
Using CAMLJS, I got this far: (CompanyNo is equivalent to Nav_CustomerNo)
var query = new CamlBuilder()
.View(["Title", ...])
.InnerJoin("ClientLookup", "c1")
.Select("EMail", "c1Email")
.InnerJoin("ClientLookup", "c2")
.Select("Nav_CustomerNo", "c2CompanyNo")
.Query()
.Where()
.All()
.ToString()
But I'm not sure how to proceed from here. How do I convert the
Customer AS c1 ON t.CustomerEMail = c1.EMail
line? I am thinking something like:
.Where("c1Email").EqualTo(??
Here is the equivalent CAML:
<View>
<ViewFields>
<FieldRef Name="Title" />
<FieldRef Name="Ticket_MainBody" />
<FieldRef Name="Ticket_SupportID" />
<FieldRef Name="ClientLookup" />
<FieldRef Name="IsPrivateTicket" />
<FieldRef Name="Ticket_IsFAQ" />
</ViewFields>
<Joins>
<Join Type="INNER" ListAlias="c1">
<Eq>
<FieldRef Name="ClientLookup" RefType="ID" />
<FieldRef Name="ID" List="c1" />
</Eq>
</Join>
<Join Type="INNER" ListAlias="c2">
<Eq>
<FieldRef Name="ClientLookup" RefType="ID" />
<FieldRef Name="ID" List="c2" />
</Eq>
</Join>
</Joins>
<ProjectedFields>
<Field ShowField="EMail" Type="Lookup" Name="c1Email" List="c1" />
<Field ShowField="Nav_CustomerNo" Type="Lookup" Name="c2CompanyNo" List="c2" />
</ProjectedFields>
<Query>
<Where />
</Query>
</View>
Just spent a considerable time this morning trying to work this out and I think I have a solution for you.
First - let's start with some generic pseudo structure:
ParentTable: Id, Title, ChildTableReference
ChildTable: Id, Title, BabyTableReference
BabyTable: Id, Title
So, we have a parent table that contains a reference. The table tied to that reference contains a reference to another table under that.
Here is a simple practical example:
(parent) CustomerOrder: Id, Title, CustomerID
(child) Customer: Id, Title, RegionID
(baby) Region: Id, Title
So, what if we wanted the following query response:
CustomerOrder_Id, CustomerOrder_Title, Customer_Title, Region_Title
The first part of the XML is easy:
<ViewFields>
<FieldRef Name='ID' />
<FieldRef Name='Title' />
<FieldRef Name='Customer_Title' />
<FieldRef Name='Region_Title' />
</ViewFields>
But how do we stitch together the tables? We use joins.
The first Join is very simple:
<Join Type='INNER' ListAlias='Customers'>
<Eq>
<FieldRef Name='CustomerID' RefType='ID' />
<FieldRef List='Customer' Name='ID' />
</Eq>
</Join>
This first join is ok and if all we wanted to do was view the customer name we would just have to add our Projection and we would be good to go. But, we also want the Region of this customer. For this, we need a second join. The trick to the second join is to add a list reference to the FIRST FieldRef statement. The exact line is: FieldRef List='Customers' Name='RegionID' RefType='ID' . Notice that the List= attribute points to the alias of the Child reference. Put another way, there is a reference list called Customers that contains a column called RegionID that is a reftype of ID. That needs to be equal to our new aliased table called Regions based on the root table Region on the ID attribute.
<Join Type='INNER' ListAlias='Regions'>
<Eq>
<FieldRef List='Customers' Name='RegionID' RefType='ID' />
<FieldRef List='Regions' Name='ID' />
</Eq>
</Join>
We are almost home now. At this point we need to create Projected Fields to spit out the Region.Title and Customer.Title attributes.
<ProjectedFields>
<Field ShowField='Title' Type='Lookup' Name='Customer_Title' List='Customers' />
<Field ShowField='Title' Type='Lookup' Name='Region_Title' List='Regions' />
</ProjectedFields>
Putting it all together then, your CAML query would be:
//Completed query
<View>
<ViewFields>
<FieldRef Name='ID' />
<FieldRef Name='Title' />
<FieldRef Name='Customer_Title' />
<FieldRef Name='Region_Title' />
</ViewFields>
<Joins>
<Join Type='INNER' ListAlias='Customers'>
<Eq>
<FieldRef Name='CustomerID' RefType='ID' />
<FieldRef List='Customer' Name='ID' />
</Eq>
</Join>
<Join Type='INNER' ListAlias='Regions'>
<Eq>
<FieldRef List='Customers' Name='RegionID' RefType='ID' />
<FieldRef List='Regions' Name='ID' />
</Eq>
</Join>
</Joins>
<ProjectedFields>
<Field ShowField='Title' Type='Lookup' Name='Customer_Title' List='Customers' />
<Field ShowField='Title' Type='Lookup' Name='Region_Title' List='Regions' />
</ProjectedFields>
<Query />
</View>
That should get you all the way there. Also - you can query (Where clause for instance) on any element you've joined to provided the field type is supported (text, refid, number, etc). Just remember that if you are going to query on say, the ID of the Region you will need to add the ID to your ProjectedFields.
// completed query with WHERE clause filtering by BABY table ID.
<View>
<ViewFields>
<FieldRef Name='ID' />
<FieldRef Name='Title' />
<FieldRef Name='Customer_Title' />
<FieldRef Name='Region_Title' />
</ViewFields>
<Joins>
<Join Type='INNER' ListAlias='Customers'>
<Eq>
<FieldRef Name='CustomerID' RefType='ID' />
<FieldRef List='Customer' Name='ID' />
</Eq>
</Join>
<Join Type='INNER' ListAlias='Regions'>
<Eq>
<FieldRef List='Customers' Name='RegionID' RefType='ID' />
<FieldRef List='Regions' Name='ID' />
</Eq>
</Join>
</Joins>
<ProjectedFields>
<Field ShowField='Title' Type='Lookup' Name='Customer_Title' List='Customers' />
<Field ShowField='ID' Type='Lookup' Name='Region_ID' List='Regions' />
<Field ShowField='Title' Type='Lookup' Name='Region_Title' List='Regions' />
</ProjectedFields>
<Query>
<Where>
<Eq>
<FieldRef Name='Region_ID' LookupId='True' />
<Value Type='Integer'>1</Value>
</Eq>
</Where>
</Query>
</View>

Alex the CAML Query has "NO RESULTS"

My SharePoint 2010 CAML Query is not returning the expected results. It is returning nothing. What am I doing wrong?
<View>
<Query>
<Where>
<In>
<FieldRef ID="1d8376f2-5ac6-407f-9652-a74405a87846" Name="Bank_x0020_Choice" />
<Values>
<Value Type="Choice">Retail</Value>
<Value Type="Choice">Commercial</Value>
</Values>
</In>
</Where>
</Query>
<ViewFields>
<FieldRef ID="1d22ea11-1e32-424e-89ab-9fedbadb6ce1" Name="ID" />
<FieldRef ID="fa564e0f-0c70-4ab9-b863-0177e6ddd247" Name="Title" />
<FieldRef ID="1d8376f2-5ac6-407f-9652-a74405a87846" Name="Bank_x0020_Choice" />
<FieldRef ID="224ba411-da77-4050-b0eb-62d422f13d3e" Name="LinkFilename2" />
</ViewFields>
<RowLimit Paged="TRUE">1000</RowLimit>
</View>
Try to use the tool CAML Builder. With this tool you can check your CAML queries with no pre-configuration and no coding. There you can test your query, maybe there is simply an spelling error in your fieldrefs, values or names.

CAML : Cannot complete this action

It seems my following query is failing and throwing "Cannot complete this action".However when I test this query in CAML Query builder it's working fine.
<Where>
<And>
<Or>
<Eq><FieldRef Name='Participant' /><Value Type='User'>Test1</Value></Eq>
<Eq><FieldRef Name='Participant' /><Value Type='User'>Test2</Value></Eq>
<Eq><FieldRef Name='Participant' /><Value Type='User'>Test3</Value></Eq>
</Or>
<Eq><FieldRef Name='Department' /><Value Type='Text'>Positioning</Value></Eq>
</And>
</Where>
Its seem to your query there are more than two condition on <OR> tab.You can only have a maximum of two conditions within an <Or> or an <And> tag.
This element can be nested inside other And and Or elements. The server supports unlimited complicated queries. However, any given And element can have only two conjuncts; that is, only two child elements. If you need to conjoin three or more conditions, you must nest the And elements, as demonstrated by the third example in the following section.
please see here : MSDN
So you would need to rewrite your query like:
<Where>
<And>
<Or>
<Eq>
<FieldRef Name='Participant' />
<Value Type='User'>Test1</Value>
</Eq>
<Or>
<Eq>
<FieldRef Name='Participant' />
<Value Type='User'>Test2</Value>
</Eq>
<Eq>
<FieldRef Name='Participant' />
<Value Type='User'>Test3</Value>
</Eq>
</Or>
</Or>
<Eq>
<FieldRef Name='Title' />
<Value Type='Text'>Postiioning</Value>
</Eq>
</And>
</Where>

IsNull with other parms in CAML query for SharePoint 2010

Consider the folowing CAML query:
<Query>
<Where>
<And>
<Eq>
<FieldRef Name="Field1"/>
<Value Type="Text">Field value 1</Value>
</Eq>
<Eq>
<FieldRef Name="Field2"/>
<Value Type="Text">Field value 2</Value>
</Eq>
<IsNull>
<FieldRef Name="Field3"/>
</IsNull>
</And>
</Where>
SharePoint raises a Microsoft.SharePoint.SoapServer.SoapServerException. What is the problem here?
The "And" element can have only two child elements. So your query could, for example, have this structure, where "And" has "IsNull" plus a nested "And" as child elements.
<Query>
<Where>
<And>
<IsNull>
<FieldRef Name="Field3" /></IsNull>
<And>
<Eq>
<FieldRef Name="Field1" /><Value Type="Counter">field 1 value</Value>
</Eq>
<Eq>
<FieldRef Name="Field2" /><Value type="Text">field 2 value</Value>
</Eq>
</And>
</And>
</Where>
This article has some good examples: http://msdn.microsoft.com/en-us/library/ms196939.aspx

Issue in using recurrence in SSRS / Dynamic Reports on Sharepoint lists

I am using Reporting Service with SharePoint List
I am querying a calendar list, i have issue fetching recurring items. When i try to use expand recurrence i get the following issue
The query for recurrence i have is:
<RSSharePointList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListName>SomeList</ListName>
<Query>
<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />
<FieldRef Name='EndDate' />
<FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Today/></Value>
</DateRangesOverlap>
</Where>
</Query>
<QueryOptions>
<ExpandRecurrence>TRUE</ExpandRecurrence>
<CalendarDate><Today/></CalendarDate>
<ViewAttributes Scope='RecursiveAll' />
</QueryOptions>
<ViewFields>
<FieldRef Name="EventDate" />
<FieldRef Name="EndDate" />
<FieldRef Name="fRecurrence" />
<FieldRef Name="Hosted_x0020_By" />
<FieldRef Name="External_x0020_Attendees" />
<FieldRef Name="Catering_x0020_Requirements" />
<FieldRef Name="Company" />
<FieldRef Name="Recurrence_x0020_ID" />
<FieldRef Name="Room" />
<FieldRef Name="RecurrenceData" />
</ViewFields>
</RSSharePointList>
is it proper? is there any other way to handle recurrence with Reporting Service? Kindly help.
Formed each XML tag by creating a new Parameter of the type XML.