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
Author
SharePoint MVP
Visits   
Today  :   7     Total  :    7602

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>();
             try
             {
                 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)
         {
             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 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]);
                                         objConfigListKeyBEListOutput.Add(obj);
                                     }
 
 
                                 }
                             }
 
                         }
                     }
                 }
             });
             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

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

Training

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 articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

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