Create new account I forgot my password    

Using .NET to Post a New BlogPost in Wordpress
Rating: 2 user(s) have rated this article Average rating: 3.0
Posted by: Shoban Kumar, on 11/22/2009, in category "Windows Forms 2.0"
Views: this article has been read 6452 times
Abstract: In this article we will see how we can use .Net and communicate with the most popular blogging tool Wordpress and post a new blog post. Before starting the development of our application, it is very important to learn about XML-RPC because we will be using XML-RPC to communicate with a Wordpress blog.

Using .NET to Post a New BlogPost In Wordpress
 
In this article we will see how we can use .Net and communicate with the most popular blogging tool Wordpress and post a new blog post. Before starting the development of our application, it is very important to learn about XML-RPC because we will be using XML-RPC to communicate with a Wordpress blog.
So, what is XML-RPC? According to its home page
“It's a spec and a set of implementations that allow software running on disparate operating systems, running in different environments to make procedure calls over the Internet.
It's remote procedure calling using HTTP as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex data structures to be transmitted processed and returned”
Wordpress is a web application written in PHP and mostly runs on Linux servers and our application will be a .Net application which will be running on a Windows desktop. Since both run on entirely different architecture and technology, we will be using XML messages to communicate between the blog and our desktop application.
To use XML-RPC in .Net, we will be using the XML-RPC.Net library. It can be downloaded from here.
“XML-RPC.NET is a library for implementing XML-RPC Services and clients in the .NET environment, supporting versions 1.0, 1.1, and 2.0 of the .NET runtime. The library has been in development since March 2001 and is used in many open-source and business application”
 
Wordpress uses XML-RPC interface and currently supports Blogger API, metaWeblog API, and the Movable Type API. We will be using MetaWeblog API and the function newPost for creating a new post in our wordpress blog.
 
According to RFC: MetaWeblog API
 
There are three basic entry-points in the API:
metaWeblog.newPost (blogid, username, password, struct, publish) returns string
metaWeblog.editPost (postid, username, password, struct, publish) returns true
metaWeblog.getPost (postid, username, password) returns struct
The blogid, username, password and publish params are as in the Blogger API. newPost returns a string representation of the post id, again as defined by the Blogger API. The struct is where the juice is.
Let’s get started. Fire up Visual Studio.
 
Step 1: Add two Textboxes and a Button to the form. Following are the names and properties of controls.
Textbox 1 : Name : txtTitle
Textbox 2 : Name : txtpost, Multiline : True
Button : Name: btnPost
Step 2: Add reference to XMLRPC.net library
Step 3: Add the following code
C#
using CookComputing.XmlRpc;
 
public struct blogInfo
{
    public string title;
    public string description;
}
 
public interface IgetCatList
{
    [CookComputing.XmlRpc.XmlRpcMethod("metaWeblog.newPost")]
    string NewPage(int blogId, string strUserName,
        string strPassword, blogInfo content, int publish);
}
 
VB.NET
Imports CookComputing.XmlRpc
 
Public Structure blogInfo
      Public title As String
      Public description As String
End Structure
 
Public Interface IgetCatList
      <CookComputing.XmlRpc.XmlRpcMethod("metaWeblog.newPost")> _
      Function NewPage(ByVal blogId As Integer, ByVal strUserName As String, ByVal strPassword As String, ByVal content As blogInfo, ByVal publish As Integer) As String
End Interface
 
In the above code we created the struct for the response and we also created the interface for the API.
Step 4: Add the following code to the Click event of the button.
C#
blogInfo newBlogPost = default(blogInfo);
newBlogPost.title = txtTitle.Text;
newBlogPost.description = txtPost.Text;
categories = (IgetCatList)XmlRpcProxyGen.Create(typeof(IgetCatList));
clientProtocol = (XmlRpcClientProtocol)categories;
clientProtocol.Url = "http://127.0.0.1/wpl/xmlrpc.php";
string result = null;
result = "";
try
{
    result = categories.NewPage(1, "shoban", "shoban", newBlogPost, 1); MessageBox.Show("Posted to Blog successfullly! Post ID : " + result);         txtPost.Text = "";
    txtTitle.Text = "";
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
 
VB.NET
Dim newBlogPost As blogInfo = Nothing
newBlogPost.title = txtTitle.Text
newBlogPost.description = txtPost.Text
categories = CType(XmlRpcProxyGen.Create(GetType(IgetCatList)), IgetCatList)
clientProtocol = CType(categories, XmlRpcClientProtocol)
clientProtocol.Url = "http://127.0.0.1/wpl/xmlrpc.php"
Dim result As String = Nothing
result = ""
Try
      result = categories.NewPage(1, "shoban", "shoban", newBlogPost, 1)
      MessageBox.Show("Posted to Blog successfullly! Post ID : " & result)
      txtPost.Text = ""
      txtTitle.Text = ""
Catch ex As Exception
      MessageBox.Show(ex.Message)
End Try
 
In the above code, we made a call to the newPost function of the Wordpress blog via the url http://127.0.0.1/wpl/xmlrpc.php. If the new post is successful, we display the post id which is returned. Below are some of the screenshots of our application in action.
PostBlog
PostedBlog
TestedWPBlog
The entire source code of this article can be downloaded over here.









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Lobby J on Tuesday, November 24, 2009 4:37 AM
Fun to read..although i would use Live writer in any case ;)
Comment posted by Shoban Kumar on Tuesday, November 24, 2009 1:55 PM
Yeah ;-) Live Write has much more options. This is just a basic code about how Live Write works.
Comment posted by shailesh on Friday, November 27, 2009 2:14 AM
Hi,
  This is very nice blog.but would you please tell me how I can use MetaWeblog API with Asp.net ?
My Requirement is like , I will write down someline of code in Msword 2007 then i will do some formatting over there and when i click on publish button under the online service then all that line should come in  this [XmlRpcMethod("metaWeblog.newPost")] method.
    I have done many r and d on this but i could not get exact result when i made http handler for this and going to click on publish button from ms word then it showing me message that ' Word can not publish this...' and i m not executing a code which i write in my newpost method.
    So please help me to solve out this problem....

Many Thanks in Advance...
Comment posted by Vijaya Kadiyala on Friday, November 27, 2009 3:31 PM
Its good..i always like to have front-end for everything :)

Comment posted by Shoban Kumar on Sunday, November 29, 2009 1:41 PM
@vijaya
Thanks for the comment.

@Shailesh
Why dont you try Office 2010? It has inbuilt options to publish to blogs.
Comment posted by raj on Monday, January 04, 2010 7:19 AM
Is there any sample for web? in .net
Comment posted by Naveen Kumar on Tuesday, January 05, 2010 7:12 AM
this Source Code is not working. When i try to bulid this and it gives an error "Object not found" when it try to execute statment " result = categories.NewPage("XXXXXXX", "XXXXXX", newBlogPost, 1);"
Comment posted by Shoban Kumar on Tuesday, January 05, 2010 10:56 AM
@Naveen
Did you try downloading the source code and running it?

@raj
What id you mean by sample for web?
Comment posted by Brad on Monday, April 26, 2010 4:17 PM
How can I modify this to specify a category to assign to the new post.  I need to be able to give the new post a category that I've already setup in Wordpress

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER