Use configuration settings for AddIns for Dynamics GP

Use the following class as a template to create your own class to read configuration settings in a custom Dynamics Great Plains Add Ins project.
public class GPAddInSectionHandler : ConfigurationSection
{
  protected const string GPAddInSectionName = "MyGPAddIn";

  // CustomSection constructor. 
  protected GPAddInSectionHandler()
  {
  }

  /// 
  /// Use this function to retrieve the Section Config object.
  /// 
  /// 
  public static GPAddInSectionHandler GetAddInSettings()
  {
	 GPAddInSectionHandler section = null;
	 // Attempt to get section from the static ConfigurationManager object. 
	 section = (GPAddInSectionHandler)ConfigurationManager.GetSection(GPAddInSectionHandler.GPAddInSectionName);
	 
	 return section;
  }
  

  // 
  // These are some sample settings that you'd put in the derived class.
  // 
   

  [ConfigurationProperty("ImportFileName", DefaultValue = "importfile.txt", IsRequired = true)]
  [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"\\", MinLength = 1, MaxLength = 255)]
  public string ImportFileName
  {
	 get
	 {
		return (string)this["ImportFileName"];
	 }
	 set
	 {
		this["ImportFileName"] = value;
	 }
  }

  [ConfigurationProperty("HelloConfigurationCaption", DefaultValue = "Hello Dynamics GP Configuration", IsRequired = true)]
  [StringValidator(MinLength = 1, MaxLength = 60)]
  public string HelloConfigurationCaption
  {
	 get
	 {
		return (string)this["HelloConfigurationCaption"];
	 }
	 set
	 {
		this["HelloConfigurationCaption"] = value;
	 }
  }
  

  public static void VerifyAndGetSection() 
  {
	 GPAddInSectionHandler section = null;
	 //T section = null;

	 // Attempt to get section from the static ConfigurationManager object. 
	 section = (GPAddInSectionHandler)ConfigurationManager.GetSection(GPAddInSectionHandler.GPAddInSectionName);
	 //section = (T)ConfigurationManager.GetSection(GPAddInSectionHandler.GPAddInSectionName);

	 // If config section doesn't exist, create the section entry 
	 // in  and the 
	 // related target section in . 
	 if (section null)
	 {
		// Open current configuration file for writing.
		Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
		
		// create an instance of our section
		section = new GPAddInSectionHandler();

		// add the section to the configuration object.
		config.Sections.Add(GPAddInSectionHandler.GPAddInSectionName, section);

		// force the section informatin to be saved, even if it hasn't been modified.
		section.SectionInformation.ForceSave = true;

		// save the configuration
		config.Save(ConfigurationSaveMode.Full);

		// forces the system to re-read the settings from disk
		ConfigurationManager.RefreshSection(GPAddInSectionHandler.GPAddInSectionName);
	 }
  }
}

An example of how to read the setting values:
GPAddInSectionHandler config = null;
config = GPAddInSectionHandler.GetAddInSettings();
string sFile = config.ImportFileName;


Last modified by Mohit @ 4/6/2025 10:02:39 PM