As APIs for Extension Manager finalized, I decided to put together a few simple examples explaining how you can take advantage of them. Consider situation when you have an extension that uses couple user specific variables. For instance, Akismet extension by Justin Etheredge needs to know API key and URL that specific for each blogger who uses extension. Currently, blogger has to enter this information by hand in the source code or extension author has to craft his own admin UI. Chunk of code below shows how Extension Manager can make life easier. All you need is to specify parameters you want blogger to be able to maintain and let Manager know about it.

static protected ExtensionSettings _settings = null;
    
// Constructor
public AkismetExtension()
{
    // Add the event handler for the CommentAdded event
    Post.AddingComment += new EventHandler<CancelEventArgs>(Post_AddingComment);
 
    ExtensionSettings settings = new ExtensionSettings("AkismetExtension");
    settings.AddParameter("apiKey", "API Key");
    settings.AddParameter("blogUrl", "Blog URL");
    settings.IsScalar = true;
    ExtensionManager.ImportSettings(settings);
    _settings = ExtensionManager.GetSettings("AkismetExtension");
}

Once you add code above to your extension, if you go to admin panel and look at “Extensions” tab, you’ll see “Edit” link next to your extension in the list.

Clicking this link will bring screen shown below, where user can enter and save information your extension needs to know.

To use this information in your extension, you pull it out of Extension Manager as in this code:

string apiKey = _settings.GetSingleValue("apiKey");
string blogUrl = _settings.GetSingleValue("blogUrl");
 
Akismet api = new Akismet(apiKey, blogUrl, "BlogEngine.net 1.2");

In short, after upgrading to BlogEngine 1.3 you’ll be able to add admin functionality to your extension in a few minutes with few simple lines of code. Extension Manager has many more functions, but basic steps are always the same: you define your parameters and let Manager take care of the rest.

Signature

Related posts

Comments

12/19/2007 4:47:29 AM

Dude, it kicks word pres' ass!! I like it.

John Ho

12/19/2007 6:57:49 AM

That's so cool. Just out of curiosity, why do you enter the URL of the blog? Couldn't you just reference Utils.AbsoluteWebRoot?

Mads Kristensen

12/19/2007 7:14:08 AM

My guess would be that it's a little more flexible if you have blog running along side with other apps or multiple blogs and want to use single key for all. But it wasn't my decision, I've just converted already written extension...

rtur.net

12/19/2007 8:04:14 AM

Way cool! We are already using it in our Photo Gallery extensions for BlogEngine.NET. Keep up the good work.

Phil

12/19/2007 4:38:50 PM

Again, great job! As part of the Photo Gallery project, we have implemented Photo Effects as extensions. However, they operate slightly different than most Blog extensions, in the fact most of their parameter values are not provided from global settings, but from per Photo settings in the database. It would be nice to be able to also register with the ExtensionManager the parameter names and types only (no values) so that our custom administration page can use this information to build the appropriate controls for the extension behavior for particular a photo, in our case.

As part of our project, we have to implement something similar to this so it would be nice to leverage the ExtensionManager in this way.

Phil

12/20/2007 1:35:07 AM

I'll got to take a look at your project, there might be some interesting ideas for a future improvements you'll be able to benefit from.

rtur.net

12/23/2007 4:15:38 PM

Extension Manager does work correctly with scalar parameters. Two minor fixes are required.

Modify the following method in ExtensionParameter.cs:

public void UpdateScalarValue(string val)
{
if (_values == null)
_values = new StringCollection();

if (_values.Count == 0)
_values.Add(val);
else
_values[0] = val;
}

Modify the following method in Settings.ascx.cs:

void btnAdd_Click(object sender, EventArgs e)
{
if (IsValidForm())
{
foreach (Control ctl in phAddForm.Controls)
{
if (ctl.GetType().Name == "TextBox")
{
TextBox txt = (TextBox)ctl;

if (_settings.IsScalar)
_settings.UpdateScalarValue(txt.ID, txt.Text);
else
_settings.AddValue(txt.ID, txt.Text);
}
}
ExtensionManager.SaveSettings(_extensionName, _settings);
if (_settings.IsScalar)
{
InfoMsg.InnerHtml = "The values has been saved";
InfoMsg.Visible = true;
}
else
{
BindGrid();
}
}
}

Phil

1/4/2008 3:43:30 AM

Pingback from thinkedge.com

New BlogEngine.NET Extensions Coordinator

thinkedge.com

2/1/2008 11:19:18 AM

@RTur

It would be helpful if the extension can specify if it is enabled or disabled by default. And if disabled by default, then the extension must be manually enabled by the end-user. This would be helpful when deploying multiple extension at once (as part of BlogEngine.NET or via an extension pack, for example) since then uncommon or niche-type extensions can be disabled by default, including extensions which may not place nice with other extensions (maybe they do a similar thing - but only one should be active at a time).

Thanks! Keep up the good work!

Phil

Add comment


 

  Country flag

biuquoteimg
Loading



<<  July 2008  >>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Enhanced with Snapshots

Subscribe to Rtur.net