getting setting from web.config in sitecore - web-config

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:
what i'm missing here can some figure out it.

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>

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

Related

Azure Devops(VSTS) Release Pipeline Variables

Hopefully I can keep this as clear and simple as possible. I'm trying to replace variables in my web.config based upon an environment. Currently I am able to replace other variables such as the one in my connection string and app settings by putting the variable in the format "#{variablename}#. This is not working for the variable in the endpoint address.
I was previously using replace tokens to accomplish this but it must now be built into azure because I don't have any extra task in the build or in the release task that's performing the replacement. Unless it's somewhere else that I'm not aware of. I have searched everything I could find and I am not seeing a reason why the value in this section isn't replace I thought it was replacing based off of name but I don't see why this value won't replace.
This replacement works with VariableName ConnectionString
<connectionStrings>
<add name="ConnectionString" connectionString="#{ConnectionString}#" providerName="System.Data.SqlClient"/>
</connectionStrings>
This doesn't work with VariableName ServiceSoapEndPoint
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Service" />
</basicHttpBinding>
<customBinding>
<binding name="Service1">
<textMessageEncoding messageVersion="Soap1" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="#{ServiceSoapEndPoint}#" binding="basicHttpBinding"
bindingConfigu`enter code here`ration="ServiceSoap" contract="Service.ServiceSoap"
name="ServiceSoapEndPoint" />
</client>
I wasn't able to find the exact solution I was looking for so I ended up doing a work around to accomplish this. I was basically trying to have environment variables so that when I would publish it would change the endpoint address based on the environment. Since I was able to get the value to change in the appsettings element I did a work around to instantiate the soap service using the value read from the appsettings value.

How to override the keys in windows services .exe.config file through VSTS release definition

I am working on VSTS release task for deploying the Windows Services Project. Unfortunately, we are not creating any Build Definition for creating drop folder.
But, my client will provide drop folder for this project, what I need is “I want to override the keys of an existing .exe.config file” at release level.
For creating the Windows Services Deploy task,I followed this Windows Services Extension
For example my drop folder looks like below:
Many thanks for this reference article and It's a very useful for changing values in config file using Power Shell commands. I have doubt in from that reference link :
For Example, If had a Code like this :
<erecruit.tasks>
<tasks>
<task name="AA" taskName="AA">
<parameters>
<param key="connectionString">Server="XXXX"</param>
</parameters>
</task>
How to change this above connectionstring value?
You can use Tokenizer task in Release Management Utility tasks extension.
Install Release Management Utility tasks extension
Add Tokenizer with XPath/Regular expressions task to release definition (Specify Source filename and Configuration Json filename)
Config file sample:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TestKey1" value="__Token1__" />
<add key="TestKey2" value="__Token2__" />
<add key="TestKey3" value="__Token3__" />
<add key="TestKey4" value="__Token4__" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Configuration Json file (Default Environment is the environment name in release definitioin):
{
"Default Environment":{
"CustomVariables":{
"Token2":"value_from_custom2",
"Token3":"value_from_custom3"
},
"ConfigChanges":[
{
"KeyName":"/configuration/appSettings/add[#key='TestKey1']",
"Attribute":"value",
"Value":"value_from_xpath"
}
]
}
}
Then the value of TestKey1 (key) will be related to value_from_xpath and the values of TestKey2 and TestKey3 will be related to value_from_custom2 and value_from_custom3.
On the other hand, you can use release variables directly if you don’t specify Configuration Json filename.
For example, there is __TokenVariable1__ in your config file and TokenVariable1 release/environment variable in release definition, then the __TokenVariable1__ will be replaced through Tokenizer task.
A related article: Using Tokenization (Token Replacement) for Builds/Releases in vNext/TFS 2015
Update:
You also can do it through PowerShell directly.
Update configuration files using PowerShell

Logging in Identity Server 3

There seems to be a lot of people asking questions about this, and yet the folks over there have decided to close my question before it's resolved; the perception being that I'm a lazy developer and haven't read documentation - not the case: https://github.com/IdentityServer/IdentityServer3/issues/3083
I've followed the instructions here: https://identityserver.github.io/Documentation/docsv2/configuration/logging.html
but I cannot get logging to happen.
My question really at this point in time is, assuming I haven't done something wrong, does it matter that I'm firing things from a unit test method?
I have a separate unit test project which is just requesting a token and writing the response out, but I'm getting a 500 error somewhere and I'd assume logging would tell me why.
The test was working before I had some repo issues a while ago and lost some work, and I THINK I am back to where I was, but I'm sure the root cause will be something simple that I've overlooked - it usually is.
Anyway, I really hope someone can help, and not just be snotty.
Many thanks!
First adding logger to IdentityServer Configuration (if Owin, under Startup public void Configuration(IAppBuilder app) method;
Serilog.Log.Logger = new LoggerConfiguration()
.WriteTo.Trace(outputTemplate: "{Timestamp} [{Level}] ({Name}){NewLine} {Message}{NewLine}{Exception}")
.CreateLogger();
Then adding webconfig the following under configuration;
<system.diagnostics>
<sources>
<source name="Thinktecture.IdentityServer" switchValue="Information, ActivityTracing">
<listeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="trace.svclog" />
</listeners>
</source>
</sources>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="sybsListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Trace.log" />
<remove name="Default" />
</listeners>
</trace>
After running, and trying to interact with the IndentityServer, you can check the Trace.log file under your project. Don't forget selecting "Show All Files"
Based on Trace.log, can you specify the problem again if you can't solve?
I managed get logging working by wrapping my unit test in
using (var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(#"C:\Users\Richard Terris\Desktop\idsLogs.txt")
.CreateLogger())
{
It's not the best solution possibly, but it works!
Thanks for the replies!

multiple applications single config file

I'm trying to write a service and configuration application. VB/C++ 2010 I've had a number of hits on google but they largely seem to be obsolete. What I have so far is a project with a single form app and a service app. The single form app has an "app.config" file and I have added a section:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="settings.config">
</appSettings>
</configuration>
In the Solution I have added a "settings.config" file and its contents is:
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="Setting1" value="This is Setting 1 from settings.config" />
<add key="Setting2" value="This is Setting 2 from settings.config" />
<add key="ConnectionString" value="ConnectString from settings.confg" />
</appSettings>
I have added a reference to then C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\System.Configuration.dll
library in both the forms app and the service app
In the very simple forms app i have the following code
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim s As String = _
System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
TextBox1.Text = s
End Sub
It doesn't work! Now clearly I am missing something. Its probably very simple. But my limited understanding is that this is automatically configuered by the config files I have? MS in their usual helful fashion seem to only give samples for 2012 and net 4.5 or greater. I need this to work on a 2003 server (as well) so I'm limited to net 4.0
The problem here is that the line System.Configuration.ConfigurationManager.AppSettings("ConnectionString") is looking for the key ConnectionString in your application's app.config file.
The fact that you have included that file key in your app.config file doesn't magically tell the ConfigurationManager to load the settings from a different file. If that's what you want you will have to read the setting for the file key and then manually load the configuration from that file.
This has not changed since the early versions of .Net though so I'm not sure why you were conflicted by the examples.
Add reference on existing assembly in .Net section of your Add Reference Popup
But i suggest you to use connectionStrings section in your config file
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Using a separate file to maintain the connection string for entity framework

I have my connection string currently in my web.config file.
Is it possible to place it in a separate file and point entity framework to it.
I found the answer here Separate ConnectionStrings and mailSettings from web.config? Possible?:
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
With file connections.config containing
<connectionStrings>
<add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
<add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
In case anyone stumbles upon this question. You can put the connection strings in a separate config file using configSource but DONT expect the EF designer to work happily with it.
Every time to go to edit the edmx and 'Update from Database' it will ask for a new connection string and then always want to save it back to the web.config. Not ideal and for me not workable. This is the case in EF6 and previous.