Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • pbl_dk 150 posts 551 karma points
    Jul 19, 2019 @ 15:04
    pbl_dk
    0

    How to write to an umbraco config file

    Hi there forum.

    I can see in the config-file that there are some references to custom Umbraco config files. How do you write to those config-files in production? Do you use an XML/IO-approach or a umbraco custom class (some kind of ConfigurationManager)? When people make custom packages, how do they write new settings to their config-files?

     <umbracoConfiguration>
    <settings configSource="config\umbracoSettings.config" />
    <BaseRestExtensions configSource="config\BaseRestExtensions.config" />
    <FileSystemProviders configSource="config\FileSystemProviders.config" />
    <dashBoard configSource="config\Dashboard.config" />
    <HealthChecks configSource="config\HealthChecks.config" />
    

  • Ali Z 31 posts 173 karma points
    Jul 22, 2019 @ 06:58
    Ali Z
    1

    The only reason I see for someone to do this would be to adapt the configuration files values by environment for Devops purpose.

    Is it the case for you ?

    If yes, which devops tool do you use ? Azure devops, Octopus, Jenkins, other ?

  • pbl_dk 150 posts 551 karma points
    Jul 22, 2019 @ 10:27
    pbl_dk
    0

    Hi there Ali, thanks for the reply.

    I would like to change config setup in production and save it. Ie. for a custom 404 page. But I went through the umbraco source code and I found out that they are using a mix of IO/Xml and configuration manager, so I have used their approach. Thanks for answering anyway.

     internal static void SaveSetting(string key, string value)
        {
            var fileName = IOHelper.MapPath(string.Format("{0}/web.config", SystemDirectories.Root));
            var xml = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
    
            var appSettings = xml.Root.DescendantsAndSelf("appSettings").Single();
    
            var setting = appSettings.Descendants("add").FirstOrDefault(s => s.Attribute("key").Value == key);
            if (setting == null)
                appSettings.Add(new XElement("add", new XAttribute("key", key), new XAttribute("value", value)));
            else
                setting.Attribute("value").Value = value;
    
            xml.Save(fileName, SaveOptions.DisableFormatting);
            ConfigurationManager.RefreshSection("appSettings");
        }
    
  • Ali Z 31 posts 173 karma points
    Jul 23, 2019 @ 05:38
    Ali Z
    1

    I don't think it's the right way, the aim of configuration files is to store static values :x

    If you want to have dynamic settings, I would recommend using a specific part of content tree or this plugin: https://our.umbraco.com/packages/developer-tools/ui-o-matic/ to store your settings.

    Maybe the document types are more releveant as you'd be able to manage access security to the settings tree.

    Kr, Ali

  • pbl_dk 150 posts 551 karma points
    Jul 23, 2019 @ 11:24
    pbl_dk
    1

    But in this particular situation, I have to read/store a custom config setting before the application runs. UI-O-Matic looks great, I will check it out. Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft