Friday, 8 February 2013

CAML Query with Multiple Conditions


Note: We can use two conditions only with in Operator(And/Or), When we are going to use multiple condition. Example 3.

6. Four Condtion with Different Data Type



SPQuery spqry = new SPQuery();

string strQry = "<Where><And><And><Eq><FieldRef Name='AssignedTo' /><Value Type='User'>" + strCurUsr + "</Value></Eq>";
strQry += "<Leq><FieldRef Name='Modified' IncludeTimeValue='FALSE' /><Value Type='DateTime'>" + strFromDate + "</Value></Leq></And>";
strQry += "<And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strPending + "</Value></Eq>";
strQry += "<Eq><FieldRef Name='Dept' LookupId='TRUE'/><Value Type='Lookup'>2</Value></Eq></And>";               
strQry += "</And></Where>";
strQry += "<OrderBy><FieldRef Name='Modified' Ascending='False'/></OrderyBy>";
spqry.Query = strQry;



SPListItemCollection splstitemcol = splst.GetItems(spqry);



5.Multiple Condtion


<Where>
    <And>
        <Or>
            <Eq>
                <FieldRef Name='FirstName' />
                <Value Type='Text'>John</Value>
            </Eq>
            <Or>
                <Eq>
                    <FieldRef Name='LastName' />
                    <Value Type='Text'>John</Value>
                </Eq>
                <Eq>
                    <FieldRef Name='Profile' />
                    <Value Type='Text'>John</Value>
                </Eq>
            </Or>
        </Or>
        <And>      
            <Or>
                <Eq>
                    <FieldRef Name='FirstName' />
                    <Value Type='Text'>Doe</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name='LastName' />
                        <Value Type='Text'>Doe</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Profile' />
                        <Value Type='Text'>Doe</Value>
                    </Eq>
                </Or>
            </Or>
            <Or>
                <Eq>
                    <FieldRef Name='FirstName' />
                    <Value Type='Text'>123</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name='LastName' />
                        <Value Type='Text'>123</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Profile' />
                        <Value Type='Text'>123</Value>
                    </Eq>
                </Or>
            </Or>
        </And>
    </And>
</Where>





4. Four Condtion
<Where>
      <And>
         <And>
            <Eq><FieldRef Name=’Title’ />  <Value Type=’Text’>Mr</Value></Eq>
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
         </And>
         <Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
         <Eq><FieldRef Name=’Country’ /> <Value Type=’Text’>India</Value></Eq>
      </And>
   </Where>
 3. Three Condtion
 <Where>
      <And>
         <And>
            <Eq><FieldRef Name=’Title’ />  <Value Type=’Text’>Mr</Value></Eq>
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
         </And>
         <Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
      </And>
   </Where>
2. Two Condtion
<Where>
          <And>
            <Eq><FieldRef Name=’Title’ />  <Value Type=’Text’>Mr</Value></Eq>
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
         </And> 
</Where>
1. Single Condition

<Where>     
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
</Where>




2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. the Four Condtion is wrong, this is the correct way:
    http://sharepoint.stackexchange.com/questions/56815/how-to-write-caml-query-with-more-than-3-and-condition

    ReplyDelete