How To Implement a Windows Live Writer Plug-in That Checks For Missing Tags

4 בדצמבר 2010

תגיות: , , ,
אין תגובות

This post provides the technical details on how I’ve implemented the Windows Live Writer Plug-in Check For Missing Tags, which was presented here.

What Is This Plug-in Anyway?

This plug-in will remind you to add the proper tags if they are missing.

Trying to post without proper tagging when the plug-in is installed will result with the following dialog:

image_thumb[2]

Clicking on the Cancel button (which is the default) will abort the publish operation and will let you update the categories.

The list of required categories is saved in a simple text file (required_categories.txt) next to the plug-in dll.

How It Was Built?

Following are the full instruction on how to build such a plug-in.
You can find the full source here.

  1. Create new class library project named CheckMissingTagsPlugin. Make sure you use .NET framework 3.5 since .NET 4 plugins are not supported.
  2. Add reference to WindowsLive.Writer.Api, located in C:\Program Files\Windows Live\Writer.
  3. Add reference to System.Windows.Forms, located in the GAC.
  4. Create a class named CheckMissingTags that inherits from PublishNotificationHook.
  5. Apply attribute WriterPluginAttribute to the class.
    [WriterPlugin("EF0CAF97-12C2-4446-BDC4-19EE53781351", "Check For Missing Tags")]
    public class CheckMissingTags : PublishNotificationHook
    {

    }

  6. Override the method OnPrePublish. Here we can check for missing categories and notify the user.

    public override bool OnPrePublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
    {
        // get list of required categories
        string[] requiredCategories;
        if (File.Exists(FileName))
        {
            requiredCategories = File.ReadAllLines(FileName);
        }
        else
        {
            // warn about missing required categories
            string message =
                "Missing categories file: " + FileName + "\n" +
                "Post anyway?";

            DialogResult dialogResult = MessageBox.Show(
                dialogOwner,
                message,
                "Warning",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2);

            // allow post only if user selected OK
            return dialogResult == DialogResult.OK;
        }

        // get list of available categories
        IEnumerable<string> availableCategories =
            from category in publishingContext.PostInfo.Categories
            select category.Name;

        // is one of the required categories available?
        bool hasAnyRequestedCategory = availableCategories.Any(
            s => requiredCategories.Contains(s, StringComparer.OrdinalIgnoreCase));

        if (!hasAnyRequestedCategory)
        {
            string allRequiredCategories = string.Join(", ", requiredCategories);
            string message =
                "None of the required categories are available.\n" +
                "(" + allRequiredCategories + ")\n" +
                "Post without required categories?";

            // warn about missing required categories
            DialogResult dialogResult = MessageBox.Show(
                dialogOwner,
                message,
                "Warning",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2);

            // allow post only if user selected OK
            return dialogResult == DialogResult.OK;
        }

        return true;
    }

  7. Add the following line to the post build event:

    XCOPY /D /Y /R "$(TargetPath)" "%ProgramFiles%\Windows Live\Writer\Plugins\"

  8. Build project.

Note: In order for the post build event to work you need to run Visual Studio as Administrator.

That’s it for now,
Arik Poznanski.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים