Wednesday, July 18, 2012

SharePoint 2010: Using “IN” Operator In CAML

CAML contains following operators: 

  1. Eq: equal 
  2. Geq: greater than or equal 
  3. Gt: greatoer then 
  4. Leq: lower than or equal 
  5. Lt: lower than 
  6. BeginsWith 
  7. Contains 
  8. IsNull 
  9. IsNotNull 
  10. Includes (new in SharePoint 2010) 
  11. In (new in Sharepoint 2010)  
I never tried to use the “IN” Operator. I thought most of my other friends do not know about this. So here is IN” Operator CAML Query.  
<Where>
      <In>
         <FieldRef Name='ID' />
         <Values>
            <Value Type='Counter'>5</Value>
            <Value Type='Counter'>6</Value>
            <Value Type='Counter'>7</Value>
         </Values>
      </In>
   </Where>
And My C# Window Application Code 
 private void button2_Click(object sender, EventArgs e)
        {
            using (SPSite _site = new SPSite("http://home"))
            {
                using (SPWeb oWebsite = _site.OpenWeb())
                { 
                    SPList spList = oWebsite.Lists.TryGetList("Custom List");
                    if (spList != null)
                    {
                        SPQuery qry = new SPQuery();
                        qry.Query =@"<Where>
      <In>
         <FieldRef Name='ID' />
         <Values>
            <Value Type='Counter'>5</Value>
            <Value Type='Counter'>6</Value>
            <Value Type='Counter'>7</Value>
         </Values>
      </In>
   </Where>";
                        qry.ViewFields = "<FieldRef Name='ID' />";
                        SPListItemCollection listItems = spList.GetItems(qry); 
                        MessageBox.Show(listItems.Count.ToString());
                    } 
                }
            }
        }
Hope you like this post.

2 comments:

  1. Good info. Lucky me I ran across your blog by accident (stumbleupon).
    I have book-marked it for later!
    My blog post ; website

    ReplyDelete