getting setting from web.config in sitecore

Amit Sharma picture Amit Sharma · Aug 14, 2015 · Viewed 8.2k times · Source

i want to get global setting from web.config file in sitecore solution, i write setting in config file and able to see it's entry in showconfig. when i try to get it's value, it is not giving appropriate value. my code is like this:

 var newsBodyTemplateID = Sitecore.Configuration.Settings.GetSetting("NewsBody");

when i evaluate this, it giving this message: enter image description here

what i'm missing here can some figure out it.

Answer

Vlad Iobagiu picture Vlad Iobagiu · Aug 14, 2015

First of all I don't recomment to add in web.config your settings. If you want to upgrade your Sitecore than you have to merge manually your web.config.

If you still want to add setttings in web.config you need to have something like :

 <configuration>

     .....
      <appSettings>
        <add key="YourSeetings" value="your value" />
         ...
        </appSettings>

     .....
      </configuration>

From C# code you need to use

ConfigurationManager.AppSettings["YourSeetings"]

If you have your settings on section /configuration/sitecore/settings you need to use from C# code :

Sitecore.Configuration.Settings.GetSetting("yoursettingsname");

Your config file will looks like :

 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>

    <!-- General settings -->
    <settings>
        <setting name="YourSettingsFieldName" value="{1EPR25B2-98C6-45BF-B9E4-824ECAAEF499}" />
    </settings>
  </sitecore>
</configuration>