Programming the MetaWeblog API in .NET/C#

Service Station, by Aaron Skonnard

Syndication

Most modern blogging engines support the MetaWeblog API, which was defined by XML-RPC.com many years ago.  It's become one of the most popular API's for programmatically interacting with blogs because of its simplicity. Even Microsoft's Windows Live Spaces provides support for it.

I wanted to use this API recently to interact with our Community Server implementation so I started searching around for client-side implementations that would be easy to program in C#. I was surprised that I couldn't find a mainstream implementation readily available. So I followed the example on MSDN and built my own MetaWeblog library in C# on top of Cook Computing's XML-RPC.NET library.

Here's what my MetaWeblogClient class looks like (truncated for brevity):

public class MetaWeblogClient : XmlRpcClientProtocol
{
    [XmlRpcMethod("metaWeblog.getRecentPosts")]
    public Post[] getRecentPosts(string blogid, string username, string password, int numberOfPosts)
    {
        return (Post[])this.Invoke("getRecentPosts", 
new object[] { blogid, username, password, numberOfPosts }); } [XmlRpcMethod("metaWeblog.newPost")] public string newPost(string blogid, string username, string password, Post content, bool publish) { return (string)this.Invoke("newPost",
new object[] { blogid, username, password, content, publish }); }
...

With this class, you can simply make method calls like getRecentPosts, newPost, editPost, etc to interact with any blog that supports the MetaWeblog API. You will need to specify that URL to the MetaWeblog endpoint prior to making those method calls. Here's an example:

class Program
{
    static void Main(string[] args)
    {
        MetaWeblogClient blog = new MetaWeblogClient();
        blog.Url = "http://www.pluralsight.com/community/blogs/metablog.ashx";

        // here's how you post a new entry...
        Post newPost = new Post();
        newPost.dateCreated = DateTime.Now;
        newPost.title = "Test post from Metablog Api";
        newPost.description = "This is the body of the post";
        newPost.categories = new string[] { "WCF", "WF" };
        blog.newPost("blogid", "username", "password", newPost, true);

        // here's how you retrieve the most recent entries...
        Post[] posts = blog.getRecentPosts("blogid", "username", "password", 5);
        foreach (Post post in posts)
            Console.WriteLine(post.title);
    }
}

The code turns out to be wonderfully simple. So if you find yourself in the same boat as me, looking for a MetaWeblog C# implementation, feel free to download my library here. Hopefully it will save you a little bit of time!


Posted Aug 19 2008, 04:09 PM by Aaron Skonnard

Comments

Chris wrote re: Programming the MetaWeblog API in .NET/C#
on 08-19-2008 9:49 PM

Looks great, except the part where your username and password are sent in the clear text.  If you plan on using the MetaWeblog API I would encourage you to use HTTPS instead of HTTP.  It's not impossible to achieve, you just need to have a certificate (even a self signed certificate will do) and configure your blog.url specifying HTTPS...  

Otherwise it's awfully nice of you to share your password with the world...

www.dscoduc.com/.../Insecure-blogging.aspx

Christopher Steen wrote Link Listing - August 19, 2008
on 08-20-2008 4:29 PM

Link Listing - August 19, 2008

Aaron Skonnard wrote re: Programming the MetaWeblog API in .NET/C#
on 08-21-2008 7:43 PM

This is true -- you should use HTTPS for the MetaWeblog endpoint unless you're not concerned about leaking your password.

WMOC#17 - PhotoSynth, Reflector move forward - Service Endpoint wrote WMOC#17 - PhotoSynth, Reflector move forward - Service Endpoint
on 08-27-2008 8:24 PM

Pingback from  WMOC#17 - PhotoSynth, Reflector move forward - Service Endpoint

Recent Links Tagged With "mainstream" - JabberTags wrote Recent Links Tagged With "mainstream" - JabberTags
on 09-12-2008 7:04 PM

Pingback from  Recent Links Tagged With "mainstream" - JabberTags

Add a Comment

(required)  
(optional)
(required)  
Remember Me?