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.

   1: using System;


   2:  


   3: using System.Collections;


   4:  


   5: using System.Collections.Generic;


   6:  


   7: using System.Text;


   8:  


   9: using WindowsLive.Writer.Api;


  10:  


  11: using System.Windows.Forms;


  12:  


  13: namespace TechnoratiTagsPlugin 


  14:  


  15: {


  16:  


  17:     [WriterPluginAttribute


  18:  


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


  20:  


  21: "Technorati Tags",


  22:  


  23:         ImagePath = "Images.Technorati.png",


  24:  


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


  26:  


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


  28:  


  29:     [InsertableContentSourceAttribute("Technorati Tags")]


  30:  


  31: public class Plugin : ContentSource


  32:  


  33:     {


  34:  


  35: public override DialogResult CreateContent(IWin32Window dialogOwner,


  36:  


  37: ref string newContent)


  38:  


  39:         {


  40:  


  41: using (InsertForm insertForm = new InsertForm())


  42:  


  43:             {


  44:  


  45: DialogResult result = insertForm.ShowDialog();


  46:  


  47: if (result == DialogResult.OK)


  48:  


  49:                 {


  50:  


  51:                     newContent = this.GetOutput(insertForm.Tags);                    


  52:  


  53:                 }


  54:  


  55: return result;


  56:  


  57:             }


  58:  


  59:         }


  60:  


  61: private string GetOutput(List<string> Tags)


  62:  


  63:         {


  64:  


  65: StringBuilder Output = new StringBuilder


  66:  


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


  68:  


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


  70:  


  71: ArrayList TemporaryOutput = new ArrayList();


  72:  


  73: foreach (string Tag in Tags)


  74:  


  75:             {


  76:  


  77:                 TemporaryOutput.Add


  78:  


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


  80:  


  81:             }


  82:  


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


  84:  


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


  86:  


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


  88:  


  89: return Output.ToString();


  90:  


  91:         }


  92:  


  93:     }


  94:  


  95: }


  96:  





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.



source : nayyeri.net




Related Posts by Categories



Widget by Hoctro

Enter your email address:

Delivered by FeedBurner

Followers



Source Code

Tips