10

Experts,

I need to read a section of Tridion.ContentManager.config file from my Custom resolver c# code.

Any Idea How to do it?

Custom Resolving

As per the SDL Live documentation here (login required): http://sdllivecontent.sdl.com/LiveContent/content/en-US/SDL_Tridion_2011_SPONE/concept_753C261063AD4B9CA5E4F50BD4FD48E9I, I want to read value for the key “Title” for the section "My.Tridion.CustomResolving". Very similar to the example provided in the docs:

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

However, it does not really explain where the Config class comes from or how to instantiate it.

I am guessing, I will have to use class “Tridion.ContentManager.ConfigurationSettings” in the code, but not really sure.

Please help.

Thanks

Saurabh

3 Answers 3

9

I wrote a blogpost about this exact issue: http://albertromkes.com/2014/01/20/custom-resolvers-and-configuration/

In short: you have to load the Tridion.ContentManager.config file yourself from the configuration of the calling application (TcmServiceHost, Publisher, etc).

0
5

As a new answer to an old question, SDL Tridion Sites 9.1 added the ability to create and read custom configuration files as part of one or more extensions as part of the add-ons feature.

See how to create a custom Add-on configuration file.

Specifically for this question, the "Schema Titles" could be added to the specified JSON to be read and used with the corresponding custom resolver code, with the custom configuration independent of the add-on code.

4

Honestly, don't know, why do you need it (if you want save key/value pairs you can do it without Tridion configuration handlers). But if you really want it, I've made fast example. For next xml:

<My.Tridion.CustomResolving>
 <Keys>
  <add key="title"
 </Keys>
</My.Tridion.CustomResolving>

You need to have something next:

public class NameValueSectionHandler : CentralizedConfigurationSection
    {
        private const string _keyCollectionConfigurationProperty = "Keys";

        public override string Name
        {
            get
            {
                return "My.Tridion.CustomResolving";
            }
        }

        /// <summary>
        /// Gets the Keys.
        /// </summary>
        [ConfigurationProperty(_keyCollectionConfigurationProperty)]
        public KeyCollection KeyCollection
        {
            get
            {
                return (KeyCollection) this[_keyCollectionConfigurationProperty];
            }
        }
    }

 public class KeyCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new KeySettings();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((KeySettings)element).Key;
        }
    }

public class KeySettings : ConfigurationElement
    {
        private const string _keyAttributeName = "Key";
        private const string _valueAttributeName = "Value";

        [ConfigurationProperty(_keyAttributeName)]
        public string Key
        {
            get
            {
                return (string) this[_keyAttributeName];
            }
            set
            {
                this[_keyAttributeName] = value;
            }
        }

        [ConfigurationProperty(_valueAttributeName)]
        public string Value
        {
            get
            {
                return (string) this[_valueAttributeName];
            }
            set
            {
                this[_valueAttributeName] = value;
            }
        }
    }

(CentralizedConfigurationSection is from Tridion.Configuration)

Almost forgotten... After that you need do something like:

NameValueSectionHandler handler = Tridion.Configuration.ConfigurationManager.GetSection<NameValueSectionHandler>("My.Tridion.CustomResolving");

With help of this handler - you can read your configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.