RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Developing Plugins for Windows Live Writer : Page 3

Find out how to write plugins for Windows Live Writer, including how to get started, how to debug, and how to package the final plugin.

SmartContentSource Plugins
SmartContentSource plugins are more complicated than basic ContentSource plugins. The biggest difference is that ContentSource plugins insert HTML code into the blog entry—that's all—and you can't change or edit the plugin later. In contrast, a SmartContentSource plugin stores as much information about what code it's inserted as the developer wants it to, which allows users to open a completed blog entry in Writer and edit using the plugin any time.

Planning What You Need
With SmartContentSource plugins, it's often best to plan out what you want internally for your plugin, such as settings and what portions of the content you would like the end user to be able to edit if required. Determining up-front what parts you want to allow users to edit later will help you determine what settings you need for the plugin. These settings are held within the IProperties property of the ISmartContent object, which gets passed to the plugin from Live Writer.

While the examples given in this article are C#, the Live Writer API is .NET, so you're not limited to just C#.
You will also want to decide where you would like the end user to edit the content, whether the edit form is similar to the form they originally used, or whether users edit from the Writer sidebar.

Listing 2 shows the code used in the example plugin's PluginSettings class.

To write a SmartContentSource plugin, you must inherit from SmartContentSource, rather than ContentSource:

   public class HiddenText : SmartContentSource
Override CreateContent Method
Like ContentSource plugins, you need to override the CreateContent method, but this time the code's slightly different:

   public override DialogResult CreateContent
      (IWin32Window dialogOwner, 
      ISmartContent newContent)
      PluginSettings settings = 
         new PluginSettings(newContent.Properties);
      using (frmMain main = new frmMain(settings))
         DialogResult result = main.ShowDialog();
         return result;
This time, Writer does not pass you a string reference, which means whatever is highlighted in the editor gets ignored. Instead you're given an ISmartContent object. ISmartContent objects contain everything about an instance of a plugin's usage, including the IProperties property value you use when including settings. However, those properties are for that ISmartContent object and only that object—they are not global as in the previous examples.

From the code snippet, you can see that I have created a PluginSettings class, which takes in the IProperties property of the ISmartContent object; this then gets passed to the Form, which makes it easier for the Form to write the required settings to the PluginSettings class.

GeneratePublishHTML Method
In the main plugin class, there are three new methods that you can override: two of these are compulsory, the third is optional. The first of these is the GeneratePublishHTML method, which is the method where the HTML is what will be published to your actual blog. This method is compulsory. The following code snippet shows a very small example of what is required:

   public override string GeneratePublishHtml
      (ISmartContent content, 
      IPublishingContext publishingContext)
      PluginSettings settings = 
         new PluginSettings(content.Properties);
      return settings.FinalText;
In this snippet, the text that would actually be published comes from a setting that you would have written to using the CreateContent method.

The IPublishingContext object argument holds information about the type of blog you're writing to, which includes a GUID for the individual blog (this comes from Live Writer itself) and the Service Name—the current publishing service—e.g., "Windows Live Spaces", "LiveJournal", "WordPress.com", etc.

CreateEditor Method
This is the second compulsory method that you need to override. This method creates the sidebar that appears in Live Writer when a SmartContentSource object is selected in the Live Writer editor:

   public override SmartContentEditor CreateEditor
      (ISmartContentEditorSite editorSite)
      return new ContextEditor();
This method is usually just as simple as that. The ContextEditor class that I call in this snippet will be explained later in the article.

Sometimes in a plugin, what you want to display in the blog entry isn't always something that you want to appear in the editor of Live Writer, JavaScript functions for example. So sometimes you might need to have something else appear in the actual Live Writer editing area. For this you need to override the GenerateEditorHTML method:

   public override string GenerateEditorHtml
      (ISmartContent content, 
      IPublishingContext publishingContext)
      PluginSettings settings = 
         new PluginSettings(content.Properties);
      return settings.PlaceHolder;
As you can see, you get passed the same objects as the GeneratePublishHTML method, so you can use the same information for putting your HTML code into the editor.

This method is the optional method; your plugin does not require it. If this isn't overridden, Live Writer will simply use what is in the GeneratePublishHTML method.

Listing 3 shows the full code for the example plugin's main class.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date