Thursday, December 17, 2009

Reading App.Config Key Values from Feature Folder

Title: Reading App.Config Key Values from Feature Folders
Details: We had a scenario where we need to have App.config under the Feature Folder. Keeping aside the need, the requirement was to read these values from a class thats inheriting SPItemEventReceiver which means I cannot use the Feature.xml properties. One way to accomplish this is as shown below. This is just a raw code, and you can tweak the code for best practices etc.

private string GetAppSettingValue(string key)
string ret = string.Empty;
string appconfigFilePath = Path.Combine(SPUtility.GetGenericSetupPath("TEMPLATE"), @"FEATURES\FEATURENAME\App.config");

ConfigXmlDocument document = new ConfigXmlDocument();
XmlNodeList xmlNodeList = document.GetElementsByTagName("add");

for(int i=0; i < xmlNodeList.Count; i++)
if(xmlNodeList[i].Attributes["key"].Value.ToLowerInvariant() == key.ToLowerInvariant())
ret = xmlNodeList[i].Attributes["value"].Value;

return ret;

No comments: