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.
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
public struct blogInfo
public string title;
public string description;
public interface IgetCatList
string NewPage(int blogId, string strUserName,
string strPassword, blogInfo content, int publish);
Public Structure blogInfo
Public title As String
Public description As String
Public Interface IgetCatList
Function NewPage(ByVal blogId As Integer, ByVal strUserName As String, ByVal strPassword As String, ByVal content As blogInfo, ByVal publish As Integer) As String
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.
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 = "";
result = categories.NewPage(1, "shoban", "shoban", newBlogPost, 1); MessageBox.Show("Posted to Blog successfullly! Post ID : " + result); txtPost.Text = "";
txtTitle.Text = "";
catch (Exception ex)
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 = ""
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
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.
The entire source code of this article can be downloaded over here.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the pre-order of The Absolutely Awesome Book on C# and .NET. This is a concise technical eBook and will be available in PDF, ePub, and mobi.
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts in your next .NET Project or to crack your next .NET Interview.
Click here to Pre-Order this eBook at a Discounted Price!