MetaWeblog.newMediaObject and Word 2007 -- BlogId as Integer and URL as String

This is one of those posts that will be of little interest or use to my regular readers but which I felt compelled to write in order to try and save future Googlers the pain I've suffered these last few days.

It's no coincidence that on Monday I was talking about changes to the way I post blogs and that I've been asked by a customer to enable posting to their Notes-based blogging system from Word 2007. Being paid to write code you've been wanting to do in your spare time is brilliant when it happens, which is all too rarely.

The system is one I've talked about in the past and was developed to allow students to have a private blogging area (case study) within their university. As all of the students on campus are about to be upgraded to Word 2007 it's seems logical to support blogging directly from what most seem use as their preferred "text editor". Heck, it's better than them copy/pasting from Word to TinyMCE. What a mess that makes!

In the same way Windows Live Writer (WLW) works Word 2007 also uses the Metaweblog API (or any other for that matter) to post to blogs. If you've written the code to support posting from WLW then you should be able to support Word too. That's the theory at least. You'd think that the fact there's a spec for both the XML-RPC and the Metaweblog API would mean the Word developers would know how to go about using them. This is Microsoft though and that would be too easy. Instead, in a way only Microsoft can, they've re-invented the specs. Well, at least they've refused to follow them to the letter.


Problem 1 -- The First Paramater Passed to the newMediaObject Method is Meant to be a String.

If you've written your own Java code to handle uploading files, it would look something like this.

   1: public Hashtable newMediaObject(String blogid,String username,String password,Hashtable content){

The problem is that Word 2007 passes the blogid parameter as an integer. That's the first of two WTFs. To get round this one you have to write an over-loaded method just to handle calls from Word, like so:

   1: public Hashtable newMediaObject(int blogid, String username, String password, Hashtable content)
   2:         throws Exception {
   3:             return newMediaObject(Integer.toString(blogid), username, password, content);
   4: }

One problem solved. One to go.

Problem 2 -- Word Expects the newMediaObject to return the URL as a String

When XML-RPC data is returned to the calling client the XML it sends tells you what type of data it contains by wrapping it in certain tags. The XML is something like:

   1: <struct>
   2:     <member>
   3:         <name>upperBound</name>
   4:         <value>
   5:             <int>139</int>
   6:         </value>
   7:     </member>
   8:     <member>
   9:        <name>empty</name>
  10:        <value>
  11:            <boolean>false</boolean>
  12:        </value>
  13:     </member>
  14:     <member>
  15:         <name>Title</name>
  16:         <value>
  17:             <string>This is a simple test</string>
  18:         </value>
  19:     </member>
  20: </struct>


Although the Title in this example (line 17) is returned explicitly as a string it doesn't need to be. The XML-RPC spec says that any value not wrapped in a tag that tells you what type it is can be assumed to be a string.

With most blogging clients you can return basic strings without wrapping them in a <string> tag. Apart from when you return the URL to Word 2007 following a newMediaObject call. The URL must be wrapped in a <string> tag! If not you'll just get a non-explicit error from Word saying your blog service doesn't support uploading of images, which isn't of course true. It's that Word doesn't follow the spec.

To get round this I had to hack my Java inspect the XML it was about to return and make sure any URL values sent back were set as strings, like so:

   1: xml.replaceAll(
   2:         "<name>url</name><value>(\\S*)</value></member>", 
   3:         "<name>url</name><value><string>$1</string></value></member>"
   4:     );

Problem 2 solved.

Problem 3 - Character Encoding.

I've yet to confirm or work out exactly what's happening but it looks like the title of a post from Word 2007 uses a character set different to the body. More on that once I've worked it out.

In summary, as you might expect, writing code to work with Word 2007 is a major pain in the back-side. Give my Windows Live Writer any day!

I leave you with a photo of Felix uploaded via a newMediaObject call from WLW and formatted using the brilliant Polaroid picture plugin.


    • avatar
    • G0rg
    • Wed 24 Jun 2009 04:29 AM

    Hi Jake,

    that's exactly what i suspected about m$ , as usual btw...

    The fact or me is that i need to get rid of the pictures upload between word 2007 and a joomla cms . This is for one of my clients which want to use absolutely word 2007 to post his existing contents on his joomla website.

    I tought that if you're a javascript expert you could probably help me with my metaweblog.php file or maybe trying to the work around into word2007.

    I don't know exactly the way to start it.

    For the moment , I can post title and text content from word to joomla via xml-rpc plugin for joomla.

    It also upload images to the web folder but word returns a dialog box saying : cannot publish pictures for this post.

    what do you think ?

    any suggestion to help me starting my investigations ?

    Thank you


Your Comments


About This Page

Written by Jake Howlett on Wed 16 Jul 2008

Share This Page

# ( ) '


The most recent comments added:

  • avatar G0rg about 15 years ago

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social

About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content