Geeks With Blogs

News
Michael Crump Microsoft MVP, INETA Community Champion and XAML Advocate.

This tutorial will show you how to build a plug-in for Windows Live Writer. Windows Live Writer is a blogging tool that Microsoft provides for free. It includes an open API for .NET developers to create custom plug-ins. In this tutorial, I will show you how easy it is to build one.

Full source and binaries are now hosted on CodePlex.

Open VS2008 or VS2010 and create a new project. Set the target framework to 2.0, Application Type to Class Library and give it a name. In this tutorial, we are going to create a plug-in that generates a twitter message with your blog post name and a TinyUrl link to the blog post.  It will do all of this automatically after you publish your post.

image

Once, we have a new projected created. We need to setup the references.

Add a reference to the WindowsLive.Writer.Api.dll located in the C:\Program Files (x86)\Windows Live\Writer\ folder, if you are using X64 version of Windows.

image

You will also need to add a reference to

  • System.Windows.Forms
  • System.Web

from the .NET tab as well.

Once that is complete, add your “using” statements so that it looks like whats shown below:

Live Writer Plug-In "Using"
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using WindowsLive.Writer.Api;
  5. using System.Web;

Now, we are going to setup some build events to make it easier to test our custom class. Go into the Properties of your project and select Build Events, click edit the Post-build and copy/paste the following line:

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

Your screen should look like the one pictured below:

image

Next, we are going to launch an external program on debug. Click the debug tab and enter

C:\Program Files (x86)\Windows Live\Writer\WindowsLiveWriter.exe

Your screen should look like the one pictured below:

image

 

Now we have a blank project and we need to add some code. We start with adding the attributes for the Live Writer Plugin. Before we get started creating the Attributes, we need to create a GUID. This GUID will uniquely identity our plug-in.

So, to create a GUID follow the steps in VS2008/2010.

Click Tools from the VS Menu ->Create GUID

 

 

image

image

It will generate a GUID like the one listed below:

 

GUID
  1. <Guid("56ED8A2C-F216-420D-91A1-F7541495DBDA")>

We only want what’s inside the quotes, so your final product should be: "56ED8A2C-F216-420D-91A1-F7541495DBDA". Go ahead and paste this snipped into your class just above the public class.

Live Writer Plug-In Attributes
  1. [WriterPlugin("56ED8A2C-F216-420D-91A1-F7541495DBDA",
  2.    "Generate Twitter Message",
  3.    Description = "After your new post has been published, this plug-in will attempt to generate a Twitter status messsage with the Title and TinyUrl link.",
  4.    HasEditableOptions = false,
  5.    Name = "Generate Twitter Message",
  6.    PublisherUrl = "http://michaelcrump.net")]
  7. [InsertableContentSource("Generate Twitter Message")]

So far, it should look like the following:

image

 

 

Next, we need to implement the PublishNotifcationHook class and override the OnPostPublish. I’m not going to dive into what the code is doing as you should be able to follow pretty easily. The code below is the entire code used in the project.

PublishNotificationHook
  1. public class Class1 :  PublishNotificationHook
  2.  {
  3.      public override void OnPostPublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
  4.      {
  5.          if (!publish) return;
  6.          if (string.IsNullOrEmpty(publishingContext.PostInfo.Permalink))
  7.          {
  8.              PluginDiagnostics.LogError("Live Tweet didn't execute, due to blank permalink");
  9.          }
  10.          else
  11.          {
  12.  
  13.              var strBlogName = HttpUtility.UrlEncode("#blogged : " + publishingContext.PostInfo.Title);  //Blog Post Title
  14.              var strUrlFinal = getTinyUrl(publishingContext.PostInfo.Permalink); //Blog Permalink URL Converted to TinyURL
  15.              System.Diagnostics.Process.Start("http://twitter.com/home?status=" + strBlogName + strUrlFinal);
  16.  
  17.          }
  18.      }

We are going to go ahead and create a method to create the short url (tinyurl).

TinyURL Helper Method
  1. private static string getTinyUrl(string url)
  2. {
  3.     var cmpUrl = System.Globalization.CultureInfo.InvariantCulture.CompareInfo;
  4.     if (!cmpUrl.IsPrefix(url, "http://tinyurl.com"))
  5.     {
  6.         var address = "http://tinyurl.com/api-create.php?url=" + url;
  7.         var client = new System.Net.WebClient();
  8.         return (client.DownloadString(address));
  9.     }
  10.     return (url);
  11. }

Go ahead and build your project, it should have copied the .DLL into the Windows Live Writer Plugin Directory. If it did not, then you will want to check your configuration.

Once that is complete, open Windows Live Writer and select Tools-> Options-> Plug-ins and enable your plug-in that you just created.

Your screen should look like the one pictured below:

image

Go ahead and click OK and publish your blog post. You should get a pop-up with the following:

image

Hit OK and It should open a Twitter and either ask for a login or fill in your status as shown below:

image 

That should do it, you can do so many other things with the API. I suggest that if you want to build something really useful consult the MSDN pages. This plug-in that I created was perfect for what I needed and I hope someone finds it useful. 

Posted on Sunday, May 30, 2010 10:18 AM General C# | Back to top

Copyright © mbcrump | Powered by: GeeksWithBlogs.net | Join free