Write a Windows Live Writer plugin using C#

As I stated yesterday Microsoft announced Beta version of its Windows Live Writer a blog client.  Most parts of this client are written in .NET and this enabled everyone to write his own plugins for it.

Today Jayson and I were chatting about lack of Currently Playing song functionality in Live Writer (it's one of cool features in BlogJet and W.Bloggar) and decided to have a competition to write its plugin for Live Writer ourselves.  I started working on Windows Media Player plugin and he worked on Winamp.  He could finish his job and posted about it here.  But I couldn't finish writing that plugin because my Windows Media Player has some problems and I couldn't get its ActiveX reference (AxWMPLib) to work.

However I didn't want to sleep without writing a plugin and tried to write a more useful plugin to add Technorati tags to blog posts.  Here I write about the process I followed to let others start coding for Live Writer.  I created a workspace on CodePlex to write plugins for fun and share them with others.

Background

Technorati tags are a way to index your posts in Technorati index and get more traffic.  You can simply put these tags under your posts via a simple link with rel attribute set to tag.

To start with Live Writer APIs and writing a plugin for it you need to download Live Writer SDK from here.  This package contains a simple demo solution with documents about Live Writer APIs.

Steps to write a plugin for Live Writer

To write a plugin you have three options:

  1. Dialog
  2. Url
  3. Live Clipboard

Latest two options are out of the scope of this post but first one (which is more useful and will be more popular) is what I want to describe here.

To write a Dialog based plugin for Live Writer you have two choices to implement one of them or both:

  1. ContentSource
  2. SmartContentSource

First one is simpler and only adds a link to Insert section/menu of your  Live Writer client application.  SmartContentSource can help you to set more properties for your plugin by putting its visual interface on a section on your client.

In this post I implement a plugin by first choice but writing a plugin by second one isn't very different.

To start coding setup a Windows Class Library and add reference to WindowsLive.Writer.Api DLL file in your project.

Main process of implementation

Class Structure This part is as simple as setting up a class and inherit it from ContentSource base class.  Before coding the main part you can set some attributes for your class such as its identifier, name, image url and ...

You can override some methods to enable different functionality but I just override CreateContent() method.  This method is a function that gets two parameters of IWin32Window and string.  Second parameter is a reference parameter.  This method returns a DialogResult enumerator.  Reference string parameter is a value that you want to embed to your post's body.  By setting this parameter in your method you send it back to Live Writer client to embed it where cursor is placed.  If CreateContent returns Ok as DialogResult value then the string value of second parameter will be embedded in your post's body otherwise it will be ignored.

So what should I do now?  You can create user controls and WinForms for your plugin with your desired UI and options then use some development techniques to get values from user and return them to this method.  This is the simplest form of writing a plugin.  In more complicated cases you can use options to be displayed in your plugin's Options list in Live Writer client and customize the behavior of your plugin based on user's choices.

Here is my Plugin class which is the main body of my plugin.  Other codes are some WinForm codes that aren't necessary to be described here and you can check them by downloading my sample codes.  This plugin simply opens a dialog box and gets the list of space separated tags and embeds them with appropriate link in post's body.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

using WindowsLive.Writer.Api;

using System.Windows.Forms;

 

namespace TechnoratiTagsPlugin

{

    [WriterPluginAttribute

        ("887EC618-8FBE-49a5-A908-2339AF2EC721",

        "Technorati Tags",

        ImagePath = "Images.Technorati.png",

        PublisherUrl = "http://www.nayyeri.net",

        Description = "Helps you to embed Technorati tags in your blog posts")]

 

 

    [InsertableContentSourceAttribute("Technorati Tags")]

    public class Plugin : ContentSource

    {

        public override DialogResult CreateContent(IWin32Window dialogOwner,

            ref string newContent)

        {

            using (InsertForm insertForm = new InsertForm())

            {

                DialogResult result = insertForm.ShowDialog();

                if (result == DialogResult.OK)

                {

                    newContent = this.GetOutput(insertForm.Tags);                   

                }

                return result;

            }

        }

 

        private string GetOutput(List<string> Tags)

        {

            StringBuilder Output = new StringBuilder

                ("<p><strong>Technorati Tags:</strong> ");

            string TagFormat = @"<a href=""http://technorati.com/tag/{0}"" rel=""tag"">{1}</a>";

 

            ArrayList TemporaryOutput = new ArrayList();

 

            foreach (string Tag in Tags)

            {

                TemporaryOutput.Add

                    (string.Format(TagFormat, Tag, Tag));

            }

 

            Output.Append(string.Join(" - ",

                (String[])TemporaryOutput.ToArray(typeof(string))));

            Output.Append("</p>");

            return Output.ToString();

        }

    }

}

Deployment

You need to deploy your project's assmeblies to Plugins subfolder of your Windows Live Writer folder (Usually located at $Drive$\Program Files\Windows Live Writer).  To do this you should add this command to your project's Post-build events:

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

If you don't do this in your project, you need to copy your DLL file manually to that folder.

Once you deploy your project, your plugin will be added to Live Writer and you can start and enjoy using it.

Last point about deployment is if users don't have .NET Framework 2.0 installed on their systems they can't use any plugin that you've written in .NET 2.0.

Result

Here are some snapshots of my plugin.  I'll upgrade it later with more features.

Windows Live Writer Technorati Tags Plugin  

Windows Live Writer Technorati Tags Plugin

Windows Live Writer Technorati Tags Plugin

Windows Live Writer Technorati Tags Plugin

Download

You download my plugin with source code from here.

Try it yourself

Start writing plugins for Windows Live Writer and share it with others at our CodePlex workspace.  All our codes are for fun there and you can feel free to join us and write new plugin or modify exisiting one.

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

42 Comments : 08.15.06

Feedbacks

 avatar
#1
ScottGu's Blog
08.16.2006 @ 12:59 AM
Earlier this week the Windows Live team released the new Windows Live Writer blog posting and management
 avatar
#2
Rich Mercer's Blog
08.16.2006 @ 3:50 AM
Technorati Tags: Blogging Well it was only released a few days ago, but already there's some interesting
 avatar
#3
while(availableTime>0) {
08.16.2006 @ 7:04 AM
Keyvan Nayyeri created a really cool plugin for Windows Live Writer, the Technorati Tags plugin....
 avatar
#4
Joe Cheng [MSFT]
08.16.2006 @ 11:01 AM
Great stuff. Just a correction, ContentSource is simpler than SmartContentSource, I know you know this but in "Steps to write a plugin for Live Writer" you have it stated the other way around.
admin avatar
#5
Keyvan Nayyeri
08.16.2006 @ 11:43 AM

Sure,

Thanks for pointing it :-)

Corrected.

 avatar
#6
TrackBack
08.16.2006 @ 11:56 AM

(via ScottGu)

Keyvan gives a nice intro to writing plugins in C# ...
 avatar
#7
Keyvan Nayyeri
08.16.2006 @ 12:38 PM
In my previous post I introduced our new workspace on CodePlex for writing Windows Live Writer plugins
 avatar
#8
Community Server Daily News
08.16.2006 @ 1:16 PM
news of the day a grab bag for what's happening in Community Server Keyvan Nayyeri walks us through the
 avatar
#9
deedee.brainstream.net
08.16.2006 @ 2:18 PM
 avatar
#10
Blog::David
08.16.2006 @ 4:06 PM
Keyvan Nayyeri nous montre comment écrire un plugin en C# pour WLW, cet article est fort intéressant
 avatar
#11
TrackBack
08.16.2006 @ 8:53 PM
The latest plugin I’ve got for WLW is one to insert Technorati Tags into the bottom of a post ...
 avatar
#12
TrackBack
08.17.2006 @ 2:14 PM

Hi,

I'm just testing out Windows Live Writer ...

 avatar
#13
Bryant Likes's Blog
08.17.2006 @ 6:56 PM
Via Greg : Feeds.com - Write a Windows Live Writer plugin using C# However I didn't want to sleep without
 avatar
#14
Bryant Likes's Blog
08.17.2006 @ 7:55 PM
If you copy source as html (or have any html in the clipboard), hitting paste in Windows Live Writer
 avatar
#15
TrackBack
08.17.2006 @ 8:54 PM
C#Feeds.com - Write a Windows Live Writer plugin using C# ...
 avatar
#16
Mac
08.18.2006 @ 12:36 PM
Hello, your post is very useful to me. I try to write a Writer plugin, but i dont know how to debug a plugin componet in my Visual Studio 2005 Express. Could you give me some advice? thx
admin avatar
#17
Keyvan Nayyeri
08.18.2006 @ 1:03 PM
Mac, The best approach you can follow for debug (for normal developers) is to build and test your plugin. What you need is adding the command I provided in Deployment section of my post to your project's Post-build events then rebuild your project and launch Windows Live Writer and test your changes there. This is for amateur solution. If you're familiar with any unit test approach you can follow it.
 avatar
#18
Community Server Daily News
08.18.2006 @ 1:04 PM
news of the day a grab bag for what's happening in Community Server Sean Winstead grabs today's top news
 avatar
#19
Mac
08.18.2006 @ 4:43 PM
Hi Keyvan, thanks for your answer, actually I need to debug my plugin in "step in" mode like normally to debug a .exe project. But in Visual Studio 2005 Express I haven't found any settings like "Start Action" to set a hosting program for this .DLL, perhalps this cheap version doesn't support any no-basic featrue.
admin avatar
#20
Keyvan Nayyeri
08.18.2006 @ 11:50 PM
Mac, Actually I don't know much about VS Express limitations. Hopefully someone who has worked more with it can help you.
 avatar
#21
Thomas Freudenberg's Blog
08.21.2006 @ 7:33 PM
I just want to note that my last post was brought to you by Windows Live Writer and Code Syntax Highlighter
 avatar
#22
Andrej Tozon's blog
08.22.2006 @ 8:58 AM
... for my Windows Live Writer post... I just love this tool. A bunch of helpful add-ins are already
 avatar
#23
Mike Ormond's WebLog
08.24.2006 @ 5:20 AM
I have to confess that I didn't plan to install Live Writer - I was quite happy with blogging from Word...
 avatar
#24
TrackBack
08.26.2006 @ 7:52 AM
We're excited to announce that in late September, there will be a new section for Windows Live Writer plugins on the Windows Live Gallery that will allow developers
 avatar
#25
Keyvan Nayyeri
08.26.2006 @ 9:02 AM
Microsoft announced a program to gather all Windows Live Writer plugins and publish them on Live Gallery
 avatar
#26
Windows Live
08.30.2006 @ 1:27 AM
Thomas J. points to a number of new Gadgets for Spaces , including Hangman and Pong, a Mortgage rates
 avatar
#27
External News
08.30.2006 @ 2:53 AM
Thomas J. points to a number of new Gadgets for Spaces , including Hangman and Pong, a Mortgage rates
 avatar
#28
Keyvan Nayyeri
09.04.2006 @ 1:42 AM
Today our Windows Live Writer Plugins project hits its first public release, codenamed Cyclone (I inspired
 avatar
#29
Microsoft Daily News
09.10.2006 @ 5:37 AM
Thomas J. points to a number of new Gadgets for Spaces , including Hangman and Pong, a Mortgage rates
 avatar
#30
Shannon Braun's Weblog
09.18.2006 @ 10:05 PM
 avatar
#31
Keyvan Nayyeri
12.07.2006 @ 10:04 AM
We're close to a new release of our Windows Live Writer plugins project and I wanted to add a new plugin
 avatar
#32
Keyvan Nayyeri
12.11.2006 @ 8:03 AM

I'm pleased to announce the second version of our Windows Live Writer plugins collection, codenamed Gustnado.

 avatar
#33
Il Blog di Nicol
01.17.2007 @ 3:15 AM
PingBack from http://www.technacular.com/2007/02/04/tool-to-create-compelling-blog-posts-easily/
 avatar
#35
Archies
03.11.2007 @ 10:46 AM
Hi Keyvan Nayyeri...just read ur blog..I am a comuter graduate and I am currently working on a samll project in which I have to make plugins for Media Player using C#...if you could plz help me in this regard..as I am not able to initiate my project....
 avatar
#36
Rahul Soni
04.19.2007 @ 10:36 PM
I thought of posting this link about a similar stuff in VB.NET. http://blogs.msdn.com/rahulso/archive/2007/04/16/create-windows-live-writer-plugin-in-visual-basic-net.aspx Regards, Rahul
 avatar
#37
Rahul Soni
04.26.2007 @ 2:16 AM
Hi, First of all, I would like to thank you for your WLW plugin in C#. Since I am not from a C# background, I used a similar logic to create it in VB.NET and also created a WLW Plugin to insert smileys. You can have it here... http://blogs.msdn.com/rahulso/archive/tags/Windows+Live+Writer/default.aspx Thanks, Rahul
 avatar
#38
sindhu
06.24.2007 @ 12:31 AM
hi! i love the post you've written here. just what i wanted. although i do not know C++ i will give this a try, as i know C. but i want to know, if it is possible to write a plugin that will help users edit text in "web preview mode" in live writer? because i like it better that way. please do reply. thanks :)
 avatar
#39
piQM
07.04.2007 @ 3:33 PM
its not easy to find out the livewriter start parameters. Write a Windows Live Writer plugin using C# - Keyvan Nayyeri
 avatar
#40
Keyvan Nayyeri
11.08.2007 @ 10:38 PM
It's been over a year since the first release of Windows Live Writer . In August 2006, this new Live
 avatar
#41
Alireza
12.26.2007 @ 3:04 PM

Hi Mr. Nayyeri

Windows Live Writer is great tool but again IT monsters forgot Right-to-Left writers in world and did not put a button for it! Just a Align Right or Left button there is in WLW.

Would you help millions of Persian, Hebrew, Arabic, Urdu, Peshu, etc. language writers and make a Plugin for "Right-to-Left" button?

Regards

AlizzA

 avatar
#42
Tony
04.06.2008 @ 11:18 PM

hi, I was attempting to write a small program for my website www.NewsForNatives.com but it only works in theory, when I compile it i get an error, it is so frustrating! but your tutorial is helping, thanks!

Leave a Comment