From the Blogosphere
Building a Search Plugin for Windows Live Writer
Microsoft has made it brain-dead simple to build a plugin and hook into their UI
Sep. 8, 2010 04:00 PM
So, a few weeks ago, Lori asked me if I’d be up for writing a plugin for Windows Live Writer to insert DevCentral related links into her blog posts. For those that don’t know about Windows Live Writer and post to a blog regularly, you are doing yourselves a disservice by not checking it out (that is, if you are a Windows user).
I’ve messed around with some Live Writer plugins in the past and figured this wouldn’t be too big a task. In fact, Microsoft has made it brain-dead simple to build a plugin and hook into their UI. Here’s the steps I took to build ours in C# with Visual Studio 2010.
Create a Class Library Project
The first step is to create the project. In Visual Studio, go into the New Project wizard and select a Class Library project.
You’ll then want to add a reference to the WindowsLive.Writer.Api.dll assembly that can be found in the Windows Live Writer installation directory.
The main class that is created will be the one that Live Writer hooks into. You can tell Live Writer that this is the one you want to use by adding the following attributes before the class declaration
3: "DevCentral Related Links Plugin",
4: ImagePath = "DevCentralWLWPlugin.jpg",
5: HasEditableOptions = false,
6: PublisherUrl = "http://devcentral.f5.com",
7: Description = "Insert Related Links from DevCentral.f5.com into your blog post.")]
8: [InsertableContentSourceAttribute("DevCentral Links")]
9: public class DevCentralWLWPlugin : ContentSource
You’ll want to add your own GUID in for the first attribute and replace the above descriptions with the ones specific to your project. You’ll also want to make sure your class is derived from the ContentSource defined in the WindowsLive.Writer.Api namespace.
Create A Popup Form
You’ll likely want your plugin to do something and the easiest way to get input from the users is by building a Windows Form. In my code, I created new Windows Form object and named it DCSearchLookupForm and added the appropriate search and filter fields to allow the user to query for search results and select the ones they want included in their post. In my Form, I had the resulting links stored in a member variable named “outputHTML”.
The form consists of the following sections
- Search Terms – Here you can enter different combinations of search terms. Instead of making everyone figure out the special ways you can enter logical expressions with Google Search, the user just enters the fields and the search phrase will be build for you. In case you want to see what the resulting phrase is, double click on the “All These Words” label and a dialog will popup with the generated phrase.
- Search Engine – Right now this is only DevCentral but you can change this in a custom build by changing the Search Id (CSE_ID) value.
- Refinement – Google Custom Search allows you to build refinements (or subsets) of the index of your search engine. This drop down will be propulated by the refinements sent back from a simple search query when the dialog is loaded.
- Number of Links – Google limits requests to 20 items but I’ve added the ability to go up to 100 by iterating through numerous requests. So, if you can’t find the results you want in the first 10 items that are the default, you can bump this up to 100.
- Related Links – This is the output of the most recent search. All of the items are selected by default. You can deselect the ones you do not wish included in your list.
- Formatting Template – Instead of hard coding a output format, I opted to allow the user to customize it with a head, item, and footer template. The head and footer are written at the beginning and end and the item template is printed once per search result item. The substitution parameters are listed in the dialog box. There are a few other hidden ones that you can use, but you will have to dig into the source to find them B-).
- Reload Default Template – If you mucked up the template and want to start over, the “Reload Default Template” link will repopulate the fields with the default values.
Integrating With Live Writer
Once you have the form created and tested out, you’ll integrate it into Live Writer by implementing the CreateContent method. This is the method that is called by Windows Live Writer when the user clicks on the link created by your plugins InsertableContentSourceAttribute value above. In this method, I just create an instance of the form, let the user do his thing and if the OK button is pressed, insert the formatted search results into the newContent variable.
1: public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
5: DCSearchLookupForm f = new DCSearchLookupForm();
6: DialogResult dr = f.ShowDialog();
7: if (dr == DialogResult.OK)
9: newContent = f.outputHTML;
12: catch (Exception ex)
14: MessageBox.Show(ex.Message.ToString(), "Error",
15: MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
17: return DialogResult.OK;
Building A Test Application
I found it helpful when debugging to build a test application that had a button and WebBrowser control to create the form and display the returned values. This took two minutes and saved me from having to debug Windows Live Writer each time through. I included this in my project as an executable called WLWPluginTestApp and set that as the Startup Project in my solution so that I could debug right into it when making changes in the plugin.
Creating A Setup Program
Installing a Windows Live Writer plugin is as simple as copying it to the plugins directory under the Windows Live Writer installation directory but that may not be the easiest way to deliver your plugin to your users. It’s very simple to create a setup project that copies the output from the plugin project into a specified directory. Just create a simple setup program and add the “Primary Output”, “Content Files”, "and “Localized Resources” from the plugin project and change the “Application Folder” property to the Windows Live Writer plugin directory. I used “[ProgramFilesFolder]Windows Live\Writer\Plugins” in my setup.
So, if you are yearning to include some DevCentral related content in your blog posts, then go ahead and download the installation package and give it a go. I’ve also made the source code available if you want to see how the plugin was made or if you want to build one of your own. The source should give you everything you need to custom make a related links module for your own Google Custom Search Engine.
Related Articles on DevCentral
Read the original blog entry...