Specific siteConfigurations in 5.3

Just created a small but very useful tool for nearly every Sitecore 5.3 developer: a specific Site configuration option which can update keys without recycling the application pool(appSettings will cause a full Application Pool Recycle which can be very annoying).

I’ll walk trough together with you guys trough the steps which are required to use this small extention:

Create a SiteSettings.config:

Create a new blank SiteSettings.config-file in App_config-folder.

Fill the file with contents:

The structure of the file  is very simple:

<?xml version=”1.0″ encoding=”utf-8″?>
<configuration>
<siteSettings name=”website”>
<add key=”Sitecore” value=”Alive and kicking!” />
</siteSettings>

You can simply create more siteSettings-nodes and ofcourse more config-values.

Define the file in the web.config:

Just after the starttag you can include the file:

<sc.include file=”/App_Config/SiteSettings.config” />

Retrieving the settings in your code:

You can easily retrieve the settings using this small class:

public static class SiteConfigurationManager
{
/// <summary>
/// siteConfig position within the web.config
/// </summary>
private static string siteConfigNode = “/sitecore/siteSettings”

/// <summary>
/// Retrieves configValue by provided website and key.
/// </summary>
/// <param name=”site”>Name of the website</param>
/// <param name=”key”>Config key</param>
/// <returns>The value of the configkey or null</returns>
public static string GetConfigValue(string site, string key)
{
string xpath = SiteConfigurationManager.siteConfigNode + “[@name='” + site + “‘]/add[@key='” + key + “‘]”
return SiteConfigurationManager.GetValueByXPath(xpath);
}

/// <summary>
/// Retrieves configValue based on the site loaded in current context and the provided key.
/// </summary>
/// <param name=”key”>Config key</param>
/// <returns>The value of the configkey or null</returns>
public static string GetConfigValue(string key)
{
return SiteConfigurationManager.GetConfigValue(Sitecore.Context.Site.Name, key);
}

/// <summary>
/// Retrieves configValue by XPath query.
/// </summary>
/// <param name=”xpath”></param>
/// <returns></returns>
private static string GetValueByXPath(string xpath)
{
XmlNode aNode = Sitecore.Configuration.Factory.GetConfigNode(xpath);
if(aNode != null)
{
XmlAttribute anAttr = aNode.Attributes[“value”];
if(anAttr != null)
{
return anAttr.Value;
}
}
return null;
}

You now simple can use the config keys by using these methods:

//Use the Sitecore-Site in the current Context
//Returns: ‘Alive and kicking’
SiteConfigurationManager.GetConfigValue(“Sitecore”)

//Use the Sitecore-Site defined by yourself
//Returns: ‘Alive and kicking’
SiteConfigurationManager.GetConfigValue(“website”, “Sitecore”)

Wasn’t so hard, was it? No it’s time to submit this small piece of code in our Ole-workflow: let’s see if Ben, our quality dude, approves my class so it will be in the next built of the LECTRIC-Library :).

One thought on “Specific siteConfigurations in 5.3”

Comments are closed.