CAML IN operator and AND operator with multiple condition

Magesh picture Magesh · May 22, 2013 · Viewed 10.4k times · Source

The Following CAML Query Not Working for me.. I am not aware much about sharepoint platform. i am using SP 2007 and trying to use IN operator for a lookup field.

"<Where>"
                                + "<And>"
                                + "<And>"
                                + "<In>"
                                + "<FieldRef Name='Role'/>"
                                + "<Values>"
                                + "<Value Type = 'Text'>A</Value>"
                                + "<Value Type = 'Text'>B</Value>"
                                + "</Values>"
                                + "</In>"
                                + "<Leq>"
                                + "<FieldRef Name='Enddate'/><Value Type = 'DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(Dt) + " </Value>"
                                + "</Leq>"
                                + "</And>"
                                + "<Includes>"
                                + "<FieldRef Name='Menu'/><Value Type='Text'>Benefits</Value>"
                                + "</Includes>"
                                + "</And>"
                                + "</Where>";

The above query not return anything but i have values in my list for the above combination.

If I slightly modify the query like following w/o using IN then it is working fine.

"<Where>"
                                + "<And>"
                                + "<And>"
                                + "<Eq>"
                                + "<FieldRef Name='Role'/>"
                                //+ "<Values>"
                                + "<Value Type = 'Text'>A</Value>"
                                //+ "<Value Type = 'Text'>B</Value>"
                                //+ "</Values>"
                                + "</Eq>"
                                + "<Leq>"
                                + "<FieldRef Name='Enddate'/><Value Type = 'DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(Dt) + " </Value>"
                                + "</Leq>"
                                + "</And>"
                                + "<Includes>"
                                + "<FieldRef Name='Menu'/><Value Type='Text'>Benefits</Value>"
                                + "</Includes>"
                                + "</And>"
                                + "</Where>";

(Please Don't Suggest me to use CAML Query builder cuz I cannot install anything on my DEV box. And Moreover I haven't install SP WSS in my local :) )

Answer

Onots picture Onots · May 22, 2013

The IN operator for CAML was introduced in Sharepoint 2010. You cannot use it in Sharepoint 2007. To achieve the same result you will have to write it as a OR's instead.

<Or>
 <Eq>
  <FieldRef Name='Role' />
  <Value Type='Text'>A</Value>
 </Eq>
 <Eq>
  <FieldRef Name='Role' />
  <Value Type='Text'>B</Value>
 </Eq>
</Or>