SharePoint Pals
 | Sign In
How to Execute CAML Query for More than One filtering dataset records in SharePoint 2013
by Sathish Nadarajan 19 May 2015
SharePoint MVP
Today  :   5     Total  :    6894

In one of the requirement, I was supposed to retrieve the records from a List by filtering on multiple records and Columns. i.e.,

I have a configuration List, on which the columns are Key, Category and Value.

Based on the Key and Category Pair, the Value needs to be retrieved. This we can execute with the basic CAML Query. But, in my case, I was supposed to retrieve the Values based on the multiple Key and Category Collection. i.e., on the page load, we will identify, what are the Keys and Categories used on this page and we need to fetch all the Values in a single query. Basically this is to reduce the multiple hits to the configuration list and reduces the execution time.

To be more clear, the input set will be Key{Key1, Key2, Key3} and Category{Cat1, Cat2, Cat3}. I need the records which has the above set of values alone. For this, we frame the CAML Query by string builder. But, there is another efficient way of doing this using LINQ.

 public List<ConfigListKeyBE> GetConfigKeyValueCollection(List<ConfigListKeyBE> objConfigListKeyBEList, string siteURL)
             List<ConfigListKeyBE> objConfigListKeyBEListOutput = new List<ConfigListKeyBE>();
                 List<string> keyNames = (from k in objConfigListKeyBEList
                                          select k.KeyName).ToList();
                 List<string> categoryNames = (from k in objConfigListKeyBEList
                                               select k.CategoryName).ToList();
                 objConfigListKeyBEListOutput = GetConfigListKeyBE(siteURL, objConfigListKeyBEListOutput);
                 objConfigListKeyBEListOutput = (from p in objConfigListKeyBEListOutput
                                                 where keyNames.Contains(p.KeyName) && categoryNames.Contains(p.CategoryName)
                                                 select p).ToList();
                 return objConfigListKeyBEListOutput;
             catch (Exception ex)
                 throw ex;
         public List<ConfigListKeyBE> GetConfigListKeyBE(string siteURL, List<ConfigListKeyBE> objConfigListKeyBEListOutput)
                 using (SPSite site = new SPSite(siteURL))
                     SPWeb web = site.RootWeb;
                     SPList configList = web.Lists[LIST_CONFIGURATION];
                     if (configList != null)
                         if (configList.ItemCount > 0)
                             SPListItemCollection configListItemCollection = configList.GetItems();
                             if (configListItemCollection != null)
                                 if (configListItemCollection.Count > 0)
                                     foreach (SPListItem item in configListItemCollection)
                                         ConfigListKeyBE obj = new ConfigListKeyBE();
                                         obj.CategoryName = Convert.ToString(item[ConstantVariables.SC_CONFIGLIST_CONFIGUATIONCATEGORY]);
                                         obj.KeyName = Convert.ToString(item[ConstantVariables.SC_CONFIGLIST_TITLE]);
                                         obj.Value = Convert.ToString(item[ConstantVariables.SC_CONFIGLIST_CONFIGVALUE]);
             return objConfigListKeyBEListOutput;

Here what I am doing is, first we retrieve all the List Items and apply a LINQ Query on top if for the required Key-Category collection.

 objConfigListKeyBEListOutput = (from p in objConfigListKeyBEListOutput
                                                 where keyNames.Contains(p.KeyName) && categoryNames.Contains(p.CategoryName)
                                                 select p).ToList();

Instead of writing many lines of code to frame the CAML Query, the above single line will do the magic for us. Moreover, we have a question like for the first time, retrieving all the records is an expensive process. I agree with that, but we can use the session or any other storing variable, so that for all the pageloads, we can use the same input data.

Happy Coding,

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Twitter October 10, 09:26
How to Use Angular HTTP Services in SharePoint List -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation