getting setting from web.config in sitecore
Asked Answered
W

2

5

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.

Walkin answered 14/8, 2015 at 7:29 Comment(6)
Could it be that you don't have Sitecore context when you are trying to read the settingHighup
yes there i'm not able to read sitecore contextWalkin
so what should i do for read this,Walkin
We'll need more info around where you are making this call. Is it in a web service?Highup
no, it is a class library project, where i defined all modelsWalkin
Have a look at this answer to a related question https://mcmap.net/q/437576/-cannot-evaluate-expression-because-a-thread-is-stopped-at-a-point-where-garbage-collection-is-impossible. What do you actually get when you just runt the application? Exception? Empty string?Highup
C
3

That method will return settings from the Sitecore\Settings node. there is another method to get AppSettings.

Sitecore.Configuration.Settings.GetAppSetting()
Carbolated answered 14/8, 2015 at 7:37 Comment(7)
can you add the exact path of you setting node (i.e. configuration\appsettings\...`) to your questionCarbolated
i'm working in siteoore with MVC, in case of view it's working but when i try to get in different library project it's giving above defined message.Walkin
i put it in app_config/include/myconfig.configWalkin
I meant the location within the config not the file path. Try going to /sitecore/admin/showconfig.aspx to make sure the setting is where it's supposed to beCarbolated
it's at the right place, under <Settings>, and one more thing when i get it on my view it's working.Walkin
OP said he is using a Sitecore setting not an AppSetting. This does not answer the question. If it was an AppSetting he wouldn't be seeing it on the ShowConfig pageHighup
When I answered, all OP had said was "get global setting from web.config file in sitecore solution". The other info came much laterCarbolated
T
8

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>
Tibetoburman answered 14/8, 2015 at 7:58 Comment(4)
it's sitecore configuration, i'm not writing appSetting for the applicationWalkin
i'm doing same thing, but couldn't able to get settingsWalkin
OP said he is using a Sitecore setting not an AppSetting. This does not answer the question. If it was an AppSetting he wouldn't be seeing it on the ShowConfig pageHighup
This is the correct answer... if by any chance your code is not picking up the config setting make sure that when you publish to your sitecore the actual .config is being publish... click the .config file and check the properties "Copy to Output Directory" set to "Copy Always" but always take caution when doing this.Ramonitaramos
C
3

That method will return settings from the Sitecore\Settings node. there is another method to get AppSettings.

Sitecore.Configuration.Settings.GetAppSetting()
Carbolated answered 14/8, 2015 at 7:37 Comment(7)
can you add the exact path of you setting node (i.e. configuration\appsettings\...`) to your questionCarbolated
i'm working in siteoore with MVC, in case of view it's working but when i try to get in different library project it's giving above defined message.Walkin
i put it in app_config/include/myconfig.configWalkin
I meant the location within the config not the file path. Try going to /sitecore/admin/showconfig.aspx to make sure the setting is where it's supposed to beCarbolated
it's at the right place, under <Settings>, and one more thing when i get it on my view it's working.Walkin
OP said he is using a Sitecore setting not an AppSetting. This does not answer the question. If it was an AppSetting he wouldn't be seeing it on the ShowConfig pageHighup
When I answered, all OP had said was "get global setting from web.config file in sitecore solution". The other info came much laterCarbolated

© 2022 - 2024 — McMap. All rights reserved.