Geeks With Blogs

Tangible Thoughts SharePoint, MOSS? and all the other questions

I was writing an extension for my blog that uses the MetaWeblog API, so here is what I have learnt about it.

It uses XML RPC for communication. So if you are coding in .NET you will need the xml-rpc library from CookComputing.

 

XML-RPC

Firstly an Introduction to XML RPC over here (I would recommend reading the whole series posted there) so that you may get an idea of

1. XML RPC Types

2. Request

3. Response

4. Errors (a.k.a. Faults)

 

Using CookComputing XML-RPC.Net

The following link shows you how to use xml-rpc.net library from cook computing connect to the WordTracker system.

Link

MetaWeblogAPI

I would recommend you reading the MetaWeblogApi spec to have an understanding of what the API is expecting as the payload of the RPC calls.

So getting down to detail

1. Making a Post

The metaWeblog api new post method signature is

metaWeblog.newPost (blogid, username, password, struct, publish) returns string

which translates as the following into csharp decorated with Xml_rpc.net

[code:c#]

[XmlRpcMethod("metaWeblog.newPost")]
        string newPost(
            string blogid,
            string username,
            string password,
            XmlRpcStruct rpcstruct,
            bool publish);

[/code]

Please note the parameter of type XmlRpcStruct, you can use a regular struct instead of this, and XmlRpc.net will translate it into a XmlRpcStruct type.

What is mandatory for this struct to have is a title, description and link. For blogengines that don't support link pass "content" as the value.

Ok so this is fine and dandy, but what about more complex blog engines that support tags, categories etc.

What I have discovered is that a well formed struct for that satisfies this requirement is as follows.

[code:c#]

public struct MetaWeblogRpcStruct

{

        public string title;

        public string link;

        public string permaLink;

        public string description;

        public  string[] categories;

        public int mt_allow_comments;

        public string mt_keywords;

        public string wp_slug;

        public string mt_basename;

        public string pubDate;

        public string mt_excerpt;

        public bool publish;

}

[/code]

so the newpost signature now is

[code:c#]

[XmlRpcMethod("metaWeblog.newPost")]
        string newPost(
            string blogid,
            string username,
            string password,
            MetaWeblogRpcStruct mwrpcstruct,
            bool publish);

[/code]

So coming back to RPC, we need an interface that defines the metaweblog api which we could base a proxy on.

hence

[code:c#]

public interface IMetaWeblog: IXmlRpcProxy
{
     [XmlRpcMethod("metaWeblog.newPost")]
     string newPost(
         string blogid,
         string username,
         string password,
         MetaWeblogRpcStruct mwrpcstruct,
         bool publish);

}

[/code]

So now that that is done, how do you make a post.

First you need to generate the proxy

[code:c#]

IMetaWeblog metaWeblog = (IMetaWeblog)XmlRpcProxyGen.Create(typeof(IMetaWeblog));

[/code]

Then set the endpoint for the RPC call (i.e. metaweblog url of the blogengine)

[code:c#]

metaWeblog.Url = "http://localhost/blog/metaweblog.axd";

// Make a post

string ret = metaWeblog.newPost(
                blogId, UserName, Password, struct, struct.publish);

[/code]

What is returned from the metaweblog api when new post is called? A GUID to the post, you will need this if you are to edit the post later on.

2. Editing a Post

The metaweblog api signature for editing a post is

metaWeblog.editPost (postid, username, password, struct, publish) returns true

which when added to our interface looks like

[code:c#]

public interface IMetaWeblog: IXmlRpcProxy

{

     [XmlRpcMethod("metaWeblog.newPost")]

     string newPost(

         string blogid,

         string username,

         string password,

         MetaWeblogRpcStruct mwrpcstruct,

         bool publish);

[XmlRpcMethod("metaWeblog.editPost")]

bool editPost(

    string postId,

    string username,

    string password,

    MetaWeblogStruct content,

    bool publish);

}

[/code]

The procedure is the same as creating a post except the fist parameter "postId" should be the GUID of the post to update.


Crossposted from tariqayad.com

Posted on Sunday, July 5, 2009 9:55 PM .NET | Back to top


Comments on this post: The MetaBlog API (Creating and Editing Posts)

# re: The MetaBlog API (Creating and Editing Posts)
Requesting Gravatar...
Thanks a lot!
I learned much!
I am interested in XML. I will go back to learn more!
Left by ac adapter on Jun 04, 2010 12:11 PM

Your comment:
 (will show your gravatar)


Copyright © Tariq | Powered by: GeeksWithBlogs.net