Writing An Application That Opens Windows Live Writer

Everyone knows that you can easily make a plugin for Windows Live Writer, but one of the APIs that’s not as well known or spoken about is the Windows Live Writer Application API. This set of API calls will allow you to open Windows Live Writer in a particular way. What do I mean by that? Well, you can open Live Writer at the Open Post window, or the options window, as just a couple of examples. The real power with this API is the BlogThis set of calls.

Some of you might be familiar with the Blog This addins for Internet Explorer (no longer available) and Firefox, which would allow you to click on the Blog This button in your browser, and it would open up Live Writer with either the page details loaded in Writer, or the highlighted text from the webpage you were viewing. Also anyone who uses my Live Writer sidebar gadget in Vista, this uses the same principle. In this article I will show you how to write a simple Windows application that uses some of the BlogThis calls.

To start off, make sure you have the latest version of Windows Live Writer installed by going to http://writer.live.com/. Now, create a new project in Visual Studio and add a reference in that project to the Windows Live Writer COM assembly (C:\Program Files\Windows Live\Writer\WindowsLiveWriter.Application.dll) and then for using it we put:

using WindowsLiveWriterApplicationLib;

Now we have to create an instance of the WindowsLiveWriterApplicationLibClass:

        WindowsLiveWriterApplicationClass wlwapp;
        public Form1()
        {
            InitializeComponent();
            wlwapp = new WindowsLiveWriterApplicationClass();
            cbOptions.SelectedIndex = 5;
        }

Now, for this example I have just created a basic WinForms application with a series of buttons for showing the different functions, it looks like this:

This App will show a few examples, but not all, after a certain point, they’re all one and the same really, just for a different use.

We’ll start off with the basic Open Writer button. The call that we make for this one is NewPost(), as that is effectively opening Writer as if running the program from a shortcut. So the code for this is:

        private void btnOpenWriter_Click(object sender, EventArgs e)
        {
            wlwapp.NewPost();
        }

For the Open Post button, we use OpenPost():

        private void btnOpenPost_Click(object sender, EventArgs e)
        {
            wlwapp.OpenPost();
        }

Now for the Open Options button, we use the ShowOptions() call. This call can be made with no parameters, and it will default to the preferences tab in the options window. However, you can put in a string parameter to open a different tab from the options window, and you can choose from the following: "preferences", “accounts”, "blogthis", “spelling”, “glossary”, "plugins", "webproxy", and "pings". And you can see from my App that I have put this in a drop down:

Which for the code looks like:

        private void btnOpenOptions_Click(object sender, EventArgs e)
        {
            string option = cbOptions.SelectedItem.ToString();
            wlwapp.ShowOptions(option);
        }

Now, the cool thing about this API is when it comes to putting images into a blog post and there are a couple of methods you can use for Images, one is for a single image, and one is for an image-set, so for multiple images. I’ll start with the single image method first which uses the BlogThisImageFile() call:

        private void btnOpenWithImage_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string title = string.Empty;
                if (!string.IsNullOrEmpty(txbxBlogTitle.Text))
                {
                    title = txbxBlogTitle.Text;
                }
                else
                {
                    title = "My New Blog Post";
                }

                string imagefile = openFileDialog1.FileName;
                
                // Open a new post using the imagefile we just selected

                wlwapp.BlogThisImageFile(title, imagefile, txbxComment.Text);
            }
        }

And for multiple images, we use the BlogThisImageFileList() call:

        private void btnOpenWithFiles_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog2.ShowDialog();
            if (result == DialogResult.OK)
            {
                string title = string.Empty;
                if (!string.IsNullOrEmpty(txbxBlogTitle.Text))
                {
                    title = txbxBlogTitle.Text;
                }
                else
                {
                    title = "My New Blog Post";
                }

                string[] imagefiles = openFileDialog2.FileNames;

                // Open a new post with the files we selected

                wlwapp.BlogThisImageFileList(title, imagefiles, txbxComment.Text);
            }
        }

The BlogThis set of calls also has one for URLs, BlogThisLink(), which will most likely be what the Blog This add-ins for IE and FF use. The code for this is:

        private void btnOpenUrl_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txbxUrl.Text) &&
                txbxUrl.Text.StartsWith("http://"))
            {
                label3.ForeColor = Color.Black;
                string title = string.Empty;
                if (!string.IsNullOrEmpty(txbxBlogTitle.Text))
                {
                    title = txbxBlogTitle.Text;
                }
                else
                {
                    title = "My New Blog Post";
                }
                wlwapp.BlogThisLink(title, txbxUrl.Text, txbxComment.Text);
            }
            else
            {
                label3.ForeColor = Color.Red;
            }            
        }

As I mentioned, I’m not going to go into detail about the others, but I will give a quick description of them:

  • BlogThisFeedItem() – This would be used in an RSS Feed Reader program, in the way that FeedDemon uses it.
  • BlogThisHTML() – This would be used to start Writer with some preconfigured HTML in the blog entry (self explanatory really)
  • BlogThisImageUrl() – Much like the BlogThisLink() call, this deals with links again, but image links, similar to if you inserted a picture and choosing a picture from the web.
  • BlogThisSnippet() – This is another one I suspect the Blog This add-ins use. In those examples this would be where you highlighted some text on a webpage to blog about it.

There are some good examples of where these API calls have been used, and I have tried to cover the main ones in this post, although one I haven’t mentioned is one I used quite frequently, which is the Live Writer System Tray App, which works in a similar way to my sidebar gadget.

If you wish to download the source code for my example app, you can do so from my SkyDrive area.

SL

  1. #1 by Mark on March 31, 2008 - 8:37 pm

    Hey Scott,This is pretty cool.  I was looking for something like this but what I was more looking for was a way to have a link on a web page that fires WLW to edit the current page.I realize this might take a plugin but sometimes you\’re on a page on your site, realize it needs editing and want to edit it without having to go find it all over again in WLW.  It would be nice just to have a "click here to edit with WLW" link.Thanks,Mark Monica

Leave a comment