Building a plug-in for Windows Live Writer
Posted
by mbcrump
on Geeks with Blogs
See other posts from Geeks with Blogs
or by mbcrump
Published on Sun, 30 May 2010 08:18:36 GMT
Indexed on
2010/05/30
15:34 UTC
Read the original article
Hit count: 335
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.
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.
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.
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:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using WindowsLive.Writer.Api;
- 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:
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:
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
It will generate a GUID like the one listed below:
- <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.
- [WriterPlugin("56ED8A2C-F216-420D-91A1-F7541495DBDA",
- "Generate Twitter Message",
- 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.",
- HasEditableOptions = false,
- Name = "Generate Twitter Message",
- PublisherUrl = "http://michaelcrump.net")]
- [InsertableContentSource("Generate Twitter Message")]
So far, it should look like the following:
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.
- public class Class1 : PublishNotificationHook
- {
- public override void OnPostPublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
- {
- if (!publish) return;
- if (string.IsNullOrEmpty(publishingContext.PostInfo.Permalink))
- {
- PluginDiagnostics.LogError("Live Tweet didn't execute, due to blank permalink");
- }
- else
- {
- var strBlogName = HttpUtility.UrlEncode("#blogged : " + publishingContext.PostInfo.Title); //Blog Post Title
- var strUrlFinal = getTinyUrl(publishingContext.PostInfo.Permalink); //Blog Permalink URL Converted to TinyURL
- System.Diagnostics.Process.Start("http://twitter.com/home?status=" + strBlogName + strUrlFinal);
- }
- }
We are going to go ahead and create a method to create the short url (tinyurl).
- private static string getTinyUrl(string url)
- {
- var cmpUrl = System.Globalization.CultureInfo.InvariantCulture.CompareInfo;
- if (!cmpUrl.IsPrefix(url, "http://tinyurl.com"))
- {
- var address = "http://tinyurl.com/api-create.php?url=" + url;
- var client = new System.Net.WebClient();
- return (client.DownloadString(address));
- }
- return (url);
- }
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:
Go ahead and click OK and publish your blog post. You should get a pop-up with the following:
Hit OK and It should open a Twitter and either ask for a login or fill in your status as shown below:
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.
© Geeks with Blogs or respective owner