Surendra Sharma

Surendra Sharma

Search This Blog

Tuesday, October 13, 2015

How to create custom Sitecore config file and access its key’s value in C#

I am damn sure that you must sometimes face this problem during reading, finding or modifying any entry in Sitecore web.config file.

The config file that I have contains 3950 lines. WTF how to work with such a large file. Is there any way to break it so that I can make my own settings value in other config file.

Fortunetly YES. Here is the trick.

·         Create one config file like "Sample.config" in "Website\App_Config\Include" folder and make keys entries in <settings> </settings> section as

<configuration xmlns:patch="">
       <setting name="EnableLogin" value="true"/>
<setting name="mykeyName" value="KeyValue"/>

At runtime Sitecore merge all these config files and treat them as a single file and work smartly. 

And now the million dollar question, How to access it in C# code?

Simply refer GetSetting() method of Settings class as below

string strKeyValue = Sitecore.Configuration.Settings.GetSetting("mykeyName");
Please leave your comments or share this trick if it’s useful for you.

No comments:

Post a Comment