Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Developing Plugins for Windows Live Writer

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.


advertisement
indows Live Writer is a free desktop blog authoring tool from Microsoft that allows you to write an entry for your blog without having to be online and do it right from your desktop. Live Writer doesn't just support Microsoft's own blogging site (Windows Live Spaces), it supports a multitude of different blog engines—any engine that supports the MetaWeblog API or Atom Publishing Protocol. This list includes Wordpress, Community Server, LiveJournal, and Blogger. You can write your blog entry in the style of your own blog because Live Writer downloads your blog style and uses it as a template.

Whether you're someone just starting out with their first blog, or a seasoned pro, Windows Live Writer makes the blogging experience that much better. And features that don't come with the program can be added in the form of plugins that add extensibility and improve the blogging experience.

Development Environment Prerequisites
You write Live Writer plugins in .NET, but while you can use .NET 1.1, Microsoft recommends that you use .NET 2.0 or higher. You will need Visual Studio 2005 or above installed—you can use any version of Visual Studio 2005 or 2008, including the free Express Editions. You will also need to download Windows Live Writer as this has the required API DLL on which the plugins are based.

Author's Note: You can develop plugins in any .NET language, but the examples here use C#.

Writing Your Plugin
Open up Visual Studio and create a new Class Library project, giving it a meaningful name for your plugin. Rename the default class1.cs to plugin.cs (this isn't essential, but will allow you to quickly see the main plugin class).

Next you need to add the Windows Live Writer API reference: bring up the Add Reference window, point to your Windows Live Writer install folder (typically C:\Program Files\Windows Live\Writer\), and then select WindowsLive.Writer.API.dll. As a best practice for this reference, set the property of Copy Local to be false for this reference (this will stop older versions of the API DLL being copied into the Live Writer Plugins folder). Open plugin.cs and include the following using statement:

using WindowsLive.Writer.Api;

You also need to add a reference to the Windows Forms namespace, so add System.Windows.Forms as a reference and also include the following:

using System.Windows.Forms;

Now you can add the plugin's attributes: these tell Live Writer about who made the plugin, the link to the developer's Web site, the description, whether the plugin has options, and the location of the image to be used as the plugin's icon within the Live Writer UI. These details go in just after the namespace (before the main class).

Here's the code:



[WriterPlugin ("d39bba2b-9522-49b1-8731-61030ccd6c95", "My First Plugin", Description = "This is my first plugin", HasEditableOptions = false, Name = "My First Plugin", PublisherUrl = "http://www.liveside.net")] [InsertableContentSource ("Bold Text")]

Please note: The GUID is unique for each plugin and you shouldn't reuse it in other plugins as this can cause problems for Writer and may cause your plugin not to be loaded.

To add an icon for your plugin, you should include the code:

ImagePath = "icon.gif",

The value of this attribute must be the file name of the icon you wish to use. Its location is relative to where it is stored in your project. The image itself must be 20 x 18 pixels in size.

After this, you need to create your plugin class. The plugin class has to inherit from the Live Writer API. There are two choices to inherit from: ContentSource and SmartContentSource. ContentSource plugins are very basic, and will insert text only into a blog entry. The plugin cannot change that text again. SmartContentSource plugins can do much more, as you'll see later in the article. This first example inherits from the ContentSource interface:

public class LiveWriterExamplePlugin : ContentSource

You need to specify how the plugin will insert text. There are three options:

  • CreateContent (from the Insert menu).
  • CreateContentFromUrl (when pasting a URL into the blog entry area or from a Blog This action).
  • CreateContentFromLiveClipboard (using the LiveClipboard, although this method isn't widely used as there isn't much LiveClipboard content out there due to documentation).
CreateContent is the most commonly used option. The plugin code overrides all of these methods, and they are not mutually exclusive; you can have more than one type in a plugin.

Override CreateContent Method
If you look at the code below, you can see that there is a string reference that contains the content. The reference is noteworthy because:

  1. The content's initial value is whatever may have been highlighted in the editing window. If no text was highlighted, then content will be empty. If text was highlighted, then the content will be the full HTML code for the highlighted text.
  2. Whatever content equals at the end of the method is what the plugin will put back into the blog entry. In other words, content must include any HTML formatting that might be required for the plugin's functionality.
ContentSource plugins are very basic plugins and will only insert text into the blog entry. You can edit SmartContentSource plugins' content again at a later time.
Remove the base code that gets put in; it's just a placeholder. For this first example, just return DialogResult.OK. This first example will be a simple plugin that makes any highlighted text bold, so the code for the CreateContent method is simply:

public override DialogResult CreateContent (IWin32Window dialogOwner, ref string content) { if (!string.IsNullOrEmpty(content)) content = string.Format( "<b>{0}</b>", content); return DialogResult.OK; }

 
Figure 1. Copying Plugins: Here's the command line you need to get the plugin to copy to the plugins folder.
Using this code, the plugin will accept whatever text was highlighted, and surround it with bold HTML tags to either end of that content. Listing 1 shows just how simple the code can be for a Live Writer plugin.

Compiling and Debugging
There are several little tricks to know about compiling and debugging your plugins for Live Writer as they are DLL files and so can't be run on their own. The two main tricks are for copying the built DLL to Live Writer's plugin directory and how to start Live Writer so that you can debug your plugin.

Figure 1 shows the command needed to copy the completed plugin to the Live Writer plugin directory.

XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live\Writer\Plugins\"

Author's Note: If your Program Files folder is in a different location than the Windows default, you will need to change it to match your system's configuration. Alternatively, you can simply set the output folder of the project to point to your Live Writer Plugins folder.

 
Figure 2. Debug Configuration: The figure shows the settings you'll need to debug your plugin.
Figure 2 shows the settings you need to be able to debug your plugin. You must start Windows Live Writer as an external application to debug your plugin. This will work only when your have changed your settings to match Figure 1; otherwise, the plugin won't have been copied to the plugin directory and Writer won't pick it up.

After configuring these two settings configured in Visual Studio, you can press F5 to build your project, copy it to the plugin directory, and then load Windows Live Writer with your plugin in debug mode.

Now that you are in debug mode, Figure 3, Figure 4, and Figure 5 show various stages while using the plugin. Figure 3 shows the text as it was typed, in normal font style. In Figure 4 you can see the text is highlighted, so the plugin can pick up that text and use it. Figure 5 shows the results of clicking the plugin name from the Insert section of Writer.

 
Figure 3. Normal Text: Here's some sample text to be made bold.
 
Figure 4. Highlighted Text: The plugin will pick up the highlighted text.
 
Figure 5. After Using the Plugin: Click the plugin name from Writer's Insert section to make the highlighted text bold.

Editor's Note: This article was first published in the "Windows Live" edition of CoDe Focus Magazine (2008, Vol. 5, Issue 2), and is reprinted here by permission.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap