Responsive Ads Here

Friday, October 31, 2014

How to use IN operator in CAML Query


IN operator allows us to specify multiple values in where clause.deactivation. Generally we will use IN operator in SQL.  

In CAML query also we can use IN operator.  In my recent project i have a requirement to get the announcements present in the announcements list. Here the condition is only i need to get the id with 1,2. So we can use IN query in this scenario. Because we need to specify multiple values in where clause.
Let us see how to use the same both in .Net CSOM and Server object model.
code snippet for CSOM:--

ClientContext clientContext = new ClientContext("your site"); 
Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("Announcements"); 
clientContext.Load(spList); 
clientContext.ExecuteQuery(); 

if (spList != null && spList.ItemCount > 0) 
{
   Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery(); 
   camlQuery.ViewXml = 
      @"  
             
               12 
             
              
      ";  

   ListItemCollection listItems = spList.GetItems(camlQuery); 
   clientContext.Load(listItems); 
   clientContext.ExecuteQuery(); 
}
   
Code snippet for Server object model
SPList spList = spWeb.Lists.TryGetList("Announcements"); 
if (spList != null) 
{ 
   SPQuery qry = new SPQuery(); 
   qry.Query =  @"
      
         
         
            1
            2
         
      
   "; 
   qry.ViewFields = @""; 
   SPListItemCollection listItems = spList.GetItems(qry); 
} 
   

3 comments:

  1. queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");

    queryBuilder.Append("");
    queryBuilder.Append("121000");
    queryBuilder.Append("129000");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");

    Not working this in my case

    ReplyDelete
  2. queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");

    queryBuilder.Append("");
    queryBuilder.Append("121000");
    queryBuilder.Append("129000");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");
    queryBuilder.Append("");

    ReplyDelete