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>
        <And>
         <Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
         <Eq><FieldRef Name=’Country’ /> <Value Type=’Text’>India</Value></Eq>
         </And>
      </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>




How to Use Empty Grid with Column Header & Footer


ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdata.DataSource = ds;
gvdata.DataBind();
int columncount = gvdata.Rows[0].Cells.Count;
gvdata.Rows[0].Cells.Clear();
gvdata.Rows[0].Cells.Add(new TableCell());
gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata.Rows[0].Cells[0].Text = "No Records Found";