Using a Custom List to Store Configuration Data
Deciding where to store configuration data is not as straight forward when talking about SharePoint as it is with other types of applications. With Windows Forms or console applications you have your app config file. With normal ASP.Net applications, you have the Web config file. There is a web config file in SharePoint, but in my opinion it is not the most ideal place to store configuration settings, especially if you have multiple teams with multiple people building applications for the same SharePoint installation.
The main problem with using the Web config file is that all of the custom applications the different teams are going to need to modify the same Web config file. This might not be true for custom web applications you extend off the layouts directory, but it is painfully obvious if you are working on web parts, custom event receivers, timer jobs or any other component that SharePoint can run in multiple contexts.
If you don't need to store any complex configuration, and only need to store a collection of name value pairs, consider storing the configuration in a custom SharePoint List. You can configure the list so that only administrators can view and manage it. (Just make sure that you run with elevated privledges when accessing it, if you go this route) You won't have to worry whether the context has access to the physical location of a file.
What I usually end up doing is building a simple list with two columns: "key" and "value." I then build a custom class that reads the list in the constructor and stores all the key/values in properties of the class. Another options is just to store everything in a HashTable like the following example:
public class ListConfiguration
{
Dictionary<string, string> keyValues = new Dictionary<string,string>();public SampleConfiguration(SPWeb web)
{
SPList config = web.Lists[Constants.LIST_CONFIG];
foreach (SPListItem item in config.Items)
{
string key = getItemString(item, Constants.COL_KEY);
string val = getItemString(item, Constants.COL_VALUE);
keyValues[key] = value;
}
}private string getItemString(SPListItem item, string key)
{
if (item[key] != null)
{
return item[key].ToString();
}
return "";
}//Accessor Method exposes the Dictionary
public string this[string key]
{
get { return keyValues[key] }
}}
So if all you need is to store string key value paris, like you would in the appSettings section of your config file, consider this approach as it might be easier to manage and maintain than other approaches.