any way to reset ExtensionSettings to default values in init?

Topics: Controls
Jun 23, 2010 at 10:42 PM

Hi,

New to BlogEngine, was looking to see if there is a way to purposely reset extensionsettings values to default in the extension constructor?

I would like to dynamically populate a dropdown in the settings everytime the extension is reloaded, but as it stands the drop down gets the default values the first time, then never changes those values.

thx in advance

Coordinator
Jun 24, 2010 at 2:54 AM

You can change default selected value in drop down any time, if that is what you looking for. This is from ExtensionSettings.cs in the core:

  /// <summary>
  /// Updates selected value in the Lists
  /// </summary>
  /// <param name="parameterName">Parameter name</param>
  /// <param name="val">Parameter value</param>
  public void UpdateSelectedValue(string parameterName, string val)

It will not change value itself, just make it selected. You can dynamically load new values if you wish and then set default to any of them.

Jun 24, 2010 at 5:06 AM

Take the following example

 Private Sub initSettings()

        Dim settings As New ExtensionSettings(Me.GetType.ToString)

        settings.IsScalar = True

        settings.AddParameter("RandomValue", "Pick Random Value", 0, True, True, ParameterType.DropDown)
        Dim myValues(4) As String
        For i As Int16 = 0 To myValues.Length - 1
            myValues(i) = "random_" & Strings.Left(Guid.NewGuid.ToString, 1) & Strings.Right(Guid.NewGuid.ToString, 1)
        Next
        settings.AddValue("RandomValue", myValues, myValues(2))

        ExtensionManager.ImportSettings(settings)
        _settings = ExtensionManager.InitSettings(Me.GetType.ToString, settings)

    End Sub

The first time I load it, I get 5 random values in the drop down (w/ the 3rd one selected)

what I want is everytime the extension is reloaded (either by recompile or by server/application cycle) I want it to load 5 new values into the settings when you go to the settings screen

Jun 24, 2010 at 5:19 AM

to expand, currently when you reload the extension (modify the source so it recompiles), I get the same 5 values, I even modified the source and doubled the size of the array, the dropdown continues to be the same 5 original default values.

Jun 25, 2010 at 9:17 AM

Correct me if I'm wrong, but doesn't the ExtensionManager maintain a repository of settings for an extension going forward even if the extension.cs file is removed from the App_Code directory?

If so wouldn't those ExtensionManager values for this extension need to be deleted in order for the extension to effectively "reset" its values?

Coordinator
Jun 25, 2010 at 5:34 PM

@w1kedz Just to clarify: you want 5 new values added to that setting every initialization, right?

If so, you might end up with rather large drop down pretty soon. Extensions reloaded every time application is reloaded. It does happen when you re-compile DLL, but it also happen when IIS decides to reload application pool for it's own reasons.

But if this is your intend anyways, you don't need to "reset" extension but rather add your values to existing setting and update selected.

Jun 25, 2010 at 6:00 PM

no... 

I want to remove existing values and add 5 new values... :)

Let me give you the full rundown, as the example above I suppose is confusing.

The extension will have the settings values being pulled from a database.  If the options in the database change I want the options in the dropdown to always match.  So if I remove a value from my database table, I want the settings dropdown to also remove the value on next reload (if the currently selected value is removed, I would programmatically select a new default, otherwise I'd like to maintain the selected value as well)

so.. say we start out with:
los angeles
new york city
memphis

in the database values table, and we want to add a value
phoenix

then the next time the extension restarts the value phoenix should be available

now say we want to remove
los angeles

then the next time the extension restarts the value los angeles should be gone

I could just manually edit the xml of the extension in my code, but I was wondering if there was a way to cleanly do this in the framework