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:
- Dialog
- Url
- 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:
- ContentSource
- 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
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
("
Technorati Tags: ");
string TagFormat = @"{1}";
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("
");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.
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
'개발 관련 글' 카테고리의 다른 글
[C#] 글 읽어주는 메모장 만들기 (0) | 2011.08.02 |
---|---|
[C#] 닷넷에서의 전역후킹 / 이벤트 날리기 (0) | 2011.08.02 |
1. 소통의 관문, 회원가입/로그인 (0) | 2010.12.14 |
윈도우폰 개발 환경설정 (0) | 2010.11.17 |
해킹방지를 위한 코딩 백과사전 (0) | 2010.08.18 |