Windows Live Writer Plugin – Options

In this post, I will talk about setting options, or preferences, for your plugin.  This is different to the properties as it relates to the plugin on a global/shared level instead of just on the content-specific level in the blog post.

If you haven’t read my first post, then that would be a good place to start.  To start with, you need to create the standard C# class library, reference the API and System.Windows.Forms, finally creating the attribute.

The first task is to set the HasEditableOptions = true in the WriterPluginAttribute. This will then enable the Options button in Live Writer’s preference tab for the plugin.

The second task is to override the EditOptions menu which is executed when the button is clicked.

public override void EditOptions(System.Windows.Forms.IWin32Window dialogOwner)
{
   OptionsForm of = new OptionsForm();
   of.Show();
}

OptionsForm is a simple WinForm.

Now we have the basic structure in place, we need some way, and place, to save and access the options set.  Because this is global, we cannot use the ISmartContent.Properties like we did in my previous post.

The API does offer a IProperties object which can be used to store and receive settings for the plugin.  This works in a similar fashion to ISmartContent.

To start with, we need to gain access to the object.  It is actually passed into an Initialize method when the plugin is created, we can simply override this method and store a reference to the object, before calling the base method.

private IProperties options = null;
public override void Initialize(IProperties pluginOptions)
{
    options = pluginOptions;
    base.Initialize(pluginOptions);
}
  

On the OptionsForm, I override the constructor to accept a IProperties object.

private IProperties options = null;
public OptionsForm(IProperties options)
{
    this.options = options;
    InitializeComponent();
}

Now I have access to the properties, I add a textbox and a button onto the form and hook up the saving of the value.

private void button1_Click(object sender, EventArgs e)
{
    options.SetString(“SettingsValue”, textBox1.Text);
    Close();
}

Finally, we need to output something.  Because we are inheriting from a ContentSource object, we need to override CreateContent.  I simple try to output the contents of the TextBox to the blog post.

public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
{
    content = options.GetString(“SettingsValue”, “Options not set”);
    return DialogResult.OK;
}

One last change, in the constructor of the form, we need to populate the values of the existing settings

textBox1.Text = options.GetString(“SettingsValue”, “”);

And that’s it, you now have an option which is stored for the lifetime of the plugin which can be accessed when inserting content.

Tip: Wrap the options with your own class to provide strongly typed access.

Download Solution – LiveWriterOptions.zip

Technorati tags:

Leave a Reply

Your email address will not be published. Required fields are marked *