Using the TridionConfigurationManager

Have you are ever been in the situation where you need to write your own DD4T templates? I don’t mean views, mind you, but real, modular Tridion templates.

I have had to write quite a few over the recent years, for example to generate sitemaps. Sometimes I needed to read a configuration value in those templates. The first option is always to use a template parameter, but they have some disadvantages. The most important one being that you always override the parameters (along with the rest of the template) if you migrate the template from DEV to TEST/PREPROD/PROD. If the parameter happens to contain environment specific information, this can create annoying issues.

But there’s another way to configure such a template: through the TridionConfigurationManager. This is a little-known gem in the world of DD4T. It’s a class that goes searching for a configuration setting in different places. Its usage is very straightforward:

string mySetting = DD4T.Templates.Base.Utils.TridionConfigurationManager.GetInstance(engine, package).AppSettings["MySetting"];

As you see, you need to pass in the Tridion template engine and package. These variables are always available in a Tridion template class.

The TridionConfigurationManager looks for the requested setting in the following places:

  1. A file called DD4T.config in the Tridion config folder
  2. Publication metadata

The DD4T.config file must be structured as a .NET application/web config with an appSettings section. For example:

<configuration>
  <appSettings>
    <key add="MySetting" value="This is the value of MySetting" /
  </appSettings>
</configuration>

If there is no DD4T.config available, the configuration manager looks for a publication metadata field with the name of the requested setting (‘MYSETTING’ in this example) and returns the value of that field.

Happy DD4T’ing!