Not logged in - Login

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.

(References page http://dynamicsgpgeek.blogspot.com/2009/10/accessing-dynamicsexeconfig.html)

   public class GPAddInSectionHandler : ConfigurationSection
   {
      protected const string GPAddInSectionName = "MyGPAddIn";

      // CustomSection constructor. 
      protected GPAddInSectionHandler()
      {
      }

      /// <summary>
      /// Use this function to retrieve the Section Config object.
      /// </summary>
      /// <returns></returns>
      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 <configSections> and the 
         // related target section in <configuration>. 
         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;