Search code examples
dotnetnuke-7

DotNetNuke Extension/Portal Settings Page


I am using the Christoc Templates in order to build a custom DNN 7 module in Visual Studio 2012. I have been able to figure out how to set module scoped settings, and tab scoped ones as well. However, what I would like to do is to set some default settings at the Extension/Package level or at least at the Portal level. That way if a user is adding a module instance, there will be default values that I can read in to use. Two questions:

1) I can certainly write a control (ASCX) called ExtensionSettings.ascx which would probably do the trick (at least for Portal scope), but how would I specify in DNN manifest file that this would be added to the proper settings page (Presumably under Host | Extensions | MyModule | Edit)?

2) How can I prepopulate these settings at installation? Do I need to add a SQL statement to add them manually or there a way to add settings through the manifest?


Solution

    1. In my experience, there is no control registration at the portal level for module settings. Only Module and TabModule scoped settings, like you said.
    2. My favorite way of doing settings that allows for a 'default' is this:

    Create a settings interface:

    public interface ISettingsRepository
    {
        string ReplyToAddress { get; set; }
        string InviteEmailSubject { get; set; }
    }
    

    And then an implementation class (Notice the default value is the default returned in the getter). See the full implementation that writes the attribute value as a module setting.

    public class SettingsRepository : ISettingsRepository
    {
        /*
         * See the full code at 
         * https://dnnsocialinvite.codeplex.com/SourceControl/latest#Components/SettingsRepository.cs
        */
        ...
    
        public string ReplyToAddress
        {
            get { return ReadSetting<string>("ReplyToAddress", "[email protected]"); }
            set { WriteSetting("ReplyToAddress", value.ToString()); }
        }
    
        public string InviteEmailSubject
        {
            get { return ReadSetting<string>("InviteEmailSubject", "You are invited to join"); }
            set { WriteSetting("InviteEmailSubject", value.ToString()); }
        }
    }
    

    Then you can use this in your module's settings control. Notice that I can reference the settings as attributes to set and get them. If I am getting one that hasn't been set, then the default value will be returned:

    public override void LoadSettings()
    {
        SettingsRepository _settingsCtrl = SettingsRepository(this.ModuleId, this.TabModuleId);
        txtEmailSubject.Text = _settingsCtrl.InviteEmailSubject;
    }
    
    public override void UpdateSettings()
    {
        SettingsRepository _settingsCtrl = SettingsRepository(this.ModuleId, this.TabModuleId);
        _settingsCtrl.InviteEmailSubject = txtEmailSubject.Text;
    }
    

    Now, this implementation has a hardcoded default for the extension. You could read the default setting string from a resource (.resx) file and maybe maintain the resource file per portal using the portal id in the resource file name. That value could replace the hard-coded default in the setting attribute's getter.