Windows Live Writer Technical Preview Version の新しいプラグイン形式、Publish Notification Hook プラグインを作ってみます。 Blogの投稿前と投稿後に処理を行うことができます。
コーディングは驚くほど簡単です。
- クラスライブラリプロジェクトを作成。
- WLWインストールフォルダにあるWindowsLive.Writer.Api.dll および .NETのSystem.Windows.Formsの参照を追加。
- クラスにWriterPlugin属性を付け、PublishNotificationHookクラスを継承。
- OnPostPublish、OnPrePublishメソッドをオーバーライド。
- ビルドして、インストールフォルダ下Pluginsフォルダにできたdllファイルをコピー。
という流れです。コードはこちら。
Imports System.Windows.Forms
Imports WindowsLive.Writer.Api
<WriterPlugin("ae75cea3-cf7d-406c-94a8-2e94bb76e264", "PublishNotificationHookPluginSample")> _
Public Class PublishNotificationHookPluginSample
Inherits PublishNotificationHook
Public Overrides Sub OnPostPublish(ByVal dialogOwner As IWin32Window, ByVal properties As IProperties, ByVal publishingContext As IPublishingContext, ByVal publish As Boolean)
End Sub
Public Overrides Function OnPrePublish(ByVal dialogOwner As IWin32Window, ByVal properties As IProperties, ByVal publishingContext As IPublishingContext, ByVal publish As Boolean) As Boolean
Return True
End Function
End Class
引数が多いので少し見にくいですが、投稿前に呼ばれるのがOnPrePublish、投稿後に呼ばれるのがOnPublishになります。使うのはこれだけ。両方とも引数は
- dialogOwner: ダイアログを表示する場合に使用するためのもの。
- properties: プラグイン独自のオプション設定を使用する場合のIPropertiesオブジェクト。従来のプラグインと同様に使います。
- publishingContext: これにBlog自体や投稿記事の情報が入っています。これを参照して何らかの処理をします。
- publish: わかりにくい名前ですが、下書き投稿の場合はFalseが入ってます。
という感じ。投稿前のOnPrePublishは戻り値を設定でき、Falseを返すと投稿をキャンセルできます。プラグインによりキャンセルされた場合、次のようなダイアログが表示されます。
投稿前のpublishingContextの中身はこんな感じ。PostInfoプロパティにほしいと思われる情報が入っていますね。
投稿後のpublishingContext。注目はPermalink部分。投稿された記事のURLが入っています。サンプルに付属しているTwitterプラグインはこれを使用しているわけですね。
詳しくは、リファレンスがSDKに含まれているのでそちらを参考にしてください。