Hi,
In this post I am going to show how to have separate sections for development, staging and production environment in web.config in asp.net / dynamics CRM Plugin. Same can be put in dynamics crm record with multi line field in (for instance custom configuration entity)
Below are steps to achieve this functionality
1) Web.config
Web.config will look like this
2) Code
public class ABCGroups
{
public static ABCGroupConfigsSection _Config = ConfigurationManager.GetSection("MyConfigs") as ABCGroupConfigsSection;
public static ABCElementCollection GetSection(string name)
{
try
{
if (name == "DEV")
return _Config.DEVGroups;
else if (name == "STAG")
return _Config.STAGGroups;
else if (name == "PROD")
return _Config.PRODGroups;
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
return null;
}
}
public class ABCGroupConfigsSection : ConfigurationSection
{
//Fill the property with the tag of respective collection.
[ConfigurationProperty("DEV")]
public ABCElementCollection DEVGroups
{
get { return (ABCElementCollection)this["DEV"]; }
}
//Fill the property with the tag of respective collection.
[ConfigurationProperty("STAG")]
public ABCElementCollection STAGGroups
{
get { return (ABCElementCollection)this["STAG"]; }
}
//Fill the property with the tag of respective collection.
[ConfigurationProperty("PROD")]
public ABCElementCollection PRODGroups
{
get { return (ABCElementCollection)this["PROD"]; }
}
}
[ConfigurationCollection(typeof(ABCGroupElement))]
public class ABCElementCollection : ConfigurationElementCollection
{
public ABCGroupElement this[int index]
{
get { return (ABCGroupElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new ABCGroupElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ABCGroupElement)element).key;
}
}
public class ABCGroupElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string key
{
get { return (string)this["key"]; }
set { this["key"] = value; }
}
[ConfigurationProperty("value", IsRequired = true)]
public string value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
3) Retrieving Key/Val for specified environment
㉺㉼㉴㉳㉽㉾㈕㈔㈆㈅㈄㈄㈃㈁㈀㉤ Ali Hamza Wadood | Microsoft Dynamics CRM Developer | Software Engineer - Microsoft Technologies(Asp.Net, Asp.Net MVC) LinkedIn
In this post I am going to show how to have separate sections for development, staging and production environment in web.config in asp.net / dynamics CRM Plugin. Same can be put in dynamics crm record with multi line field in (for instance custom configuration entity)
Below are steps to achieve this functionality
1) Web.config
Web.config will look like this
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="MyConfigs" type="MyConfigs.ABCGroupConfigsSection" />
</configSections>
<appSettings>
<add key="CurrentEnvironment" value="DEV" />
</appSettings>
<MyConfigs>
<DEV>
<add key="crmconnection" value="https://samplecrm4.dynamics.com" />
<add key="logFolderPath" value="E:\Log\" />
<add key="key1" value="blahblahblah" />
<add key="key2" value="blahblahblah" />
<add key="key3" value="blahblahblah" />
<add key="key4" value="blahblahblah" />
</DEV>
<STAG>
<add key="crmconnection" value="https://samplecrm4.dynamics.com" />
<add key="logFolderPath" value="E:\Log\" />
<add key="key1" value="blahblahblah" />
<add key="key2" value="blahblahblah" />
<add key="key3" value="blahblahblah" />
<add key="key4" value="blahblahblah" />
</STAG>
<PROD>
<add key="crmconnection" value="https://samplecrm4.dynamics.com" />
<add key="logFolderPath" value="E:\Log\" />
<add key="key1" value="blahblahblah" />
<add key="key2" value="blahblahblah" />
<add key="key3" value="blahblahblah" />
<add key="key4" value="blahblahblah" />
</PROD>
</MyConfigs>
<!-- Other configurations here -->
</configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="MyConfigs" type="MyConfigs.ABCGroupConfigsSection" />
</configSections>
<appSettings>
<add key="CurrentEnvironment" value="DEV" />
</appSettings>
<MyConfigs>
<DEV>
<add key="crmconnection" value="https://samplecrm4.dynamics.com" />
<add key="logFolderPath" value="E:\Log\" />
<add key="key1" value="blahblahblah" />
<add key="key2" value="blahblahblah" />
<add key="key3" value="blahblahblah" />
<add key="key4" value="blahblahblah" />
</DEV>
<STAG>
<add key="crmconnection" value="https://samplecrm4.dynamics.com" />
<add key="logFolderPath" value="E:\Log\" />
<add key="key1" value="blahblahblah" />
<add key="key2" value="blahblahblah" />
<add key="key3" value="blahblahblah" />
<add key="key4" value="blahblahblah" />
</STAG>
<PROD>
<add key="crmconnection" value="https://samplecrm4.dynamics.com" />
<add key="logFolderPath" value="E:\Log\" />
<add key="key1" value="blahblahblah" />
<add key="key2" value="blahblahblah" />
<add key="key3" value="blahblahblah" />
<add key="key4" value="blahblahblah" />
</PROD>
</MyConfigs>
<!-- Other configurations here -->
</configuration>
2) Code
public class ABCGroups
{
public static ABCGroupConfigsSection _Config = ConfigurationManager.GetSection("MyConfigs") as ABCGroupConfigsSection;
public static ABCElementCollection GetSection(string name)
{
try
{
if (name == "DEV")
return _Config.DEVGroups;
else if (name == "STAG")
return _Config.STAGGroups;
else if (name == "PROD")
return _Config.PRODGroups;
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
return null;
}
}
public class ABCGroupConfigsSection : ConfigurationSection
{
//Fill the property with the tag of respective collection.
[ConfigurationProperty("DEV")]
public ABCElementCollection DEVGroups
{
get { return (ABCElementCollection)this["DEV"]; }
}
//Fill the property with the tag of respective collection.
[ConfigurationProperty("STAG")]
public ABCElementCollection STAGGroups
{
get { return (ABCElementCollection)this["STAG"]; }
}
//Fill the property with the tag of respective collection.
[ConfigurationProperty("PROD")]
public ABCElementCollection PRODGroups
{
get { return (ABCElementCollection)this["PROD"]; }
}
}
[ConfigurationCollection(typeof(ABCGroupElement))]
public class ABCElementCollection : ConfigurationElementCollection
{
public ABCGroupElement this[int index]
{
get { return (ABCGroupElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new ABCGroupElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ABCGroupElement)element).key;
}
}
public class ABCGroupElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string key
{
get { return (string)this["key"]; }
set { this["key"] = value; }
}
[ConfigurationProperty("value", IsRequired = true)]
public string value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
3) Retrieving Key/Val for specified environment
public class Globals
{
private static string currentenvironmentconfigvalue = null;
public static string CurrentEnvironmentConfigValue
{
get
{
if (currentenvironmentconfigvalue == null)
currentenvironmentconfigvalue = ConfigurationManager.AppSettings["CurrentEnvironment"].ToString();
return currentenvironmentconfigvalue;
}
}
private static NameValueCollection currentsection = null;
public static NameValueCollection CurrentSection
{
get
{
if (currentsection == null)
currentsection = ConvertToNameValueCollection(ABCGroups.GetSection(CurrentEnvironmentConfigValue));
return currentsection;
// return (NameValueCollection)ConfigurationManager.GetSection(CurrentEnvironmentConfigValue);
}
}
private static NameValueCollection ConvertToNameValueCollection(ABCElementCollection coll)
{
NameValueCollection NColl = new NameValueCollection();
foreach (ABCGroupElement mg in coll)
{
NColl.Add(mg.key, mg.value);
}
return NColl;
}
}
// Using this line can retrieve specified key
Globals.CurrentSection["crmconnection"].ToString();
{
private static string currentenvironmentconfigvalue = null;
public static string CurrentEnvironmentConfigValue
{
get
{
if (currentenvironmentconfigvalue == null)
currentenvironmentconfigvalue = ConfigurationManager.AppSettings["CurrentEnvironment"].ToString();
return currentenvironmentconfigvalue;
}
}
private static NameValueCollection currentsection = null;
public static NameValueCollection CurrentSection
{
get
{
if (currentsection == null)
currentsection = ConvertToNameValueCollection(ABCGroups.GetSection(CurrentEnvironmentConfigValue));
return currentsection;
// return (NameValueCollection)ConfigurationManager.GetSection(CurrentEnvironmentConfigValue);
}
}
private static NameValueCollection ConvertToNameValueCollection(ABCElementCollection coll)
{
NameValueCollection NColl = new NameValueCollection();
foreach (ABCGroupElement mg in coll)
{
NColl.Add(mg.key, mg.value);
}
return NColl;
}
}
// Using this line can retrieve specified key
Globals.CurrentSection["crmconnection"].ToString();
㉺㉼㉴㉳㉽㉾㈕㈔㈆㈅㈄㈄㈃㈁㈀㉤ Ali Hamza Wadood | Microsoft Dynamics CRM Developer | Software Engineer - Microsoft Technologies(Asp.Net, Asp.Net MVC) LinkedIn