Generic Method to Extract a Value from Configuration List

Here I will brief you about the method which will give you a Configuration value depending upon the key passed. Of course, the key and value pair will be residing in your SharePoint list.

Being a good developer you should make your application as configurable as possible to avoid last minute rush to small changes and hence resulting in another deployment of a solution.

E.g. Consider a case where you have marked your Custom Top Navigation code to have 6 nodes(Hard coded value / taken from constants ); But due to some requirement changes you need to make it 8 nodes now.How you will do it ? Again a Code Change ? If you do; this will require another solution deployment.

Instead of that; identify these kind of parameters in your custom application and try and make those as configurable as possible which may lead to changes in future.

For this to have create a new list Named Configuration Settings at root site collection (of course you can have any name), with 3 site columns :
  1. Rename Title as Config Key : A key used in solution E.g. NumberOfTopNavigationNodes

  2. Config Value : Value for a key E.g. 6

  3. Description : Description of Key and Value or where it is pointing to. E.g. Number of top Navigation nodes used in Top Navigation web part under Controls WSP.
Note: I have used internal Name of Title column in query so rename the column aa.

Lets have a look at the code which will do it for us:
    1. /// <summary>    
    2. /// method to retrive Configration value for corresponding key    
    3. /// </summary>    
    4. /// <param name="ConfigKey">Configration Key Value</param>    
    5. /// <returns>string of Configration value</returns>    
    6. public static string GetValueForCoreSettingsKey(SPWeb web, string configKey)    
    7. {    
    8.     string resultValue = string.Empty;    
    9.     try    
    10.     {    
    11.         if (web == null)    
    12.         {    
    13.             throw new ArgumentNullException("Web");    
    14.         }    
    15.         else    
    16.         {    
    17.             SPQuery Query = new SPQuery();    
    18.             string query = "<Where><Eq><FieldRef Name = 'Title'/><Value Type='Text'>"    
    19.                            + configKey    
    20.                            + "</Value></Eq></Where>";    
    21.             Query.Query = query;    
    22.             SPList CoreConfigrationList = web.Lists.TryGetList("Configuration Settings");    
    23.             if (CoreConfigrationList != null)    
    24.             {    
    25.                 SPListItemCollection ListItemCollection = CoreConfigrationList.GetItems(Query);    
    26.     
    27.                 if (ListItemCollection != null)    
    28.                 {    
    29.                     foreach (SPListItem ListItem in ListItemCollection)    
    30.                     {    
    31.                         resultValue = ListItem["ConfigurationValue"].ToString();    
    32.                     }    
    33.                 }    
    34.             }    
    35.         }    
    36.     }    
    37.     catch (Exception)    
    38.     {    
    39.         throw;    
    40.     }    
    41.     return resultValue;    
    42. }    
Here you need to pass the parameter of SPWeb Object and Configuration Key of which you need a value.

E.g. GetValueForCoreSettingsKey(web,NumberOfTopNavigationNodes)

By this way you can keep your application configurable and easy to maintain in future. Simply changing the value for an entry will solve your purpose.

Happy SharePoint!!!