Using the DotNetKicks PlugIn for Live Writer

I mentioned last night that I wrote a DotNetKicks PlugIn for Windows Live Writer using the new June 2008 Technical Preview release that came out this week. My first pass at the plug in is now complete, though I have a laundry list of changes and improvements I want to make. I am going to make it available today on CodePlex here, if you are interested in the runtime or the source code. The source is a bit rough right now, so please no don’t throw any tomatoes at me. If you have any thoughts on enhancements, I will gladly take them into consideration.

A short disclaimer here … you need the June 2008 Technical Preview of Windows Live Writer and the SDK for this code to work. Also, this is a technical preview and the team’s blog states that at this time you cannot upload plugins made with it to the Windows Live Gallery. The downloads come with some sample applications, a new features quick start and a help file.

The new features I focused on are the 2 new plugin types of PublishNotificationHook and HeaderFooterSource. The former allows you to create a plugin that operates after the post has been published. The sample shows a Twitter plugin that tweets your new blog post to Twitter. The HeaderFooterSource type allows you to add content to the header or footer of a post. The sample shows how to add a DiggThis link to the footer. Note that this type is not visible during the design mode, but is in preview mode or once posted (of course).

For my DotNetKicks PlugIn I used the HeaderFooterSource base class and extended it. It is pretty basic right now as the new base class made it very simple to get this working.I tapped into the HeaderFooterSource class and overrode the GeneratePreviewHtml and GeneratePublishHtml methods to display the KickIt link in the footer. The key with this new code is that it now handles 3 former problem areas for me automatically:

  1. It happens just after the post is communicated with the web site
  2. It knows the post’s permalink (the url to the post)
  3. It provides a way to put the link in the footer, instead of me having to use a named DIV

I also overrode the EditOptions method and created a quick and drity config form that allows you to customize the colors of the DotNetKicks link. Here is the options form (note that this is an area I will greatly expand on in future releases):

I’ll be adding a color image on this screen to show the color selected, too. When you click the ellipses, the second dialog appears where you can choose a color. The colors you choose are persisted and allow you to produce a link like this:

The code that generates the link is pretty straightforward, as shown below:

public string GenerateLink(
    ISmartContent smartContent, 
    string url, 
    string title, 
    out Position position)
{
    smartContent.Layout.Alignment = AlignRight ? Alignment.Right : Alignment.Left;
    smartContent.Layout.TopMargin = 4;
    smartContent.Layout.BottomMargin = 4;
    smartContent.Layout.LeftMargin = 0;
    smartContent.Layout.RightMargin = 0;
 
    //var settings = new DNKSettings(smartContent.Properties);
    var dnkUrl = "http://www.dotnetkicks.com/kick/?url="+ url;
    var imgUrl = "http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=" + url +
                        string.Format("&bgcolor={0}&fgcolor={1}&border={2}&cbgcolor={3}&cfgcolor={4}",
                            _settings.KickItBackgroundColor,
                            _settings.KickItTextColor,
                            _settings.BorderColor,
                            _settings.KickCountBackgroundColor,
                            _settings.KickCountTextColor);
 
    var html = string.Format("<a href=\"{0}\">",dnkUrl) + string.Format("<img src=\"{0}\" border=0/>", imgUrl) + "</a>";
 
    _settings.Content = html;
 
    position = Position.Footer;
 
    return _settings.Content;
}

The complete source code can now be found on CodePlex. Again, feel free to leave comments here to let me know what other features you want. Again, this code is written using a tech preview so as long as you do not mind using WLW in a tech preview state, you’ll be good to go.

UPDATE (June 5, 2008) : I added a new version to CodePlex. Check it out.

And don’t forget to KickIt!