How to Execute CAML Query for More than One filtering dataset records in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
May 19, 2015
 
Rate this article
 
Views
12199

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment