Custom Resolvers and Configuration

While working on a Custom Resolver, I needed to grab some configuration values. This seems fairly straightforward, and the documentation from SDL Tridion covers this. It states that we have to add a ConfigurationSection to the ‘Tridion.ContentManager.config’ file and that we can read these values using the following code:

private string SCHEMA_TITLES = 
Config.GetConfig("My.Tridion.CustomResolving", "schemaTitles");

It’s unclear where ‘Config.GetConfig’ comes from, but there’s more. The SDL Tridion Content Manager uses different services to resolve items to be published. The following SDL Tridion services use your Custom Resolver:

– TcmServiceHost
– TcmPublisher

The TcmServiceHost calls the resolver when a user clicks on the ‘Show Items to publish’ button in the ‘Publish’ popup.
The TcmPublisher calls the resolver when the item is actually published.
Both services have their own executable and their own configuration: TcmServiceHost.exe.config and TcmPublisher.exe.config (Located in the %Tridion_Home%\bin directory)

So, after adding the configuration for our custom resolver to the Tridion.ContentManager.config file I hooked up the debugger to the TcmPublisher and clicked ‘Publish’: no configuration values were found. Which makes perfect sense, since the TcmPublisher.exe uses the TcmPublisher.exe.config as its configuration source. The same is true for the TcmServiceHost: it uses the TcmServiceHost.exe.config as its configuration source.

How to solve this configuration issue?

Well, luckily both config files have a reference to the ‘Tridion.ContentManager.config’ file: (All Tridion Content Manager executables/services have a reference to this config file)

 <tridionConfigSections>
    <sections>
      <clear />
      <add filePath="D:\Program Files (x86)\Tridion\config\Tridion.ContentManager.config" />
      <add name="loggingConfiguration" />
    </sections>
  </tridionConfigSections>

So now, in your Custom Resolver it’s nothing more then loading the Tridion.ContentManager.config file to get our custom resolver configuration value(s):


Tridion.Configuration.ConfigurationSections tcmConfigSections = (Tridion.Configuration.ConfigurationSections)ConfigurationManager.GetSection(Tridion.Configuration.ConfigurationSections.SectionName);
var tcmSectionElem = tcmConfigSections.Sections.Cast<Tridion.Configuration.SectionElement>().FirstOrDefault(s => !string.IsNullOrEmpty(s.FilePath) && s.FilePath.EndsWith("tridion.contentmanager.config", StringComparison.InvariantCultureIgnoreCase));
if(tcmSectionElem != null)
{
var tcmConfigFilePath = tcmSectionElem.FilePath;
//load Tridion.ContentManager.config
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = tcmConfigFilePath };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var myCustomResolverSettings = ((AppSettingsSection)config.GetSection("My.Tridion.CustomResolving")).Settings;
var schemaTitles = myCustomResolverSettings["schemaTitles"].Value.ToString();
}

view raw

gistfile1.cs

hosted with ❤ by GitHub

The configuration in the Tridion.ContentManager.config’ is as follows (shortened):

<section name="My.Tridion.CustomResolving" type="System.Configuration.AppSettingsSection" />
...
<My.Tridion.CustomResolving>
	<add key="schemaTitles" value="FullArticleSchema" />
</My.Tridion.CustomResolving>

The type of the ConfigurationSection is ‘AppSettingsSection’. This is different from the documentation, but that doesn’t matter.
You can insert whatever section you like, as long as you update the code to get the ConfigurationSection. (Cast it to the correct type)

Have fun!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: