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
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.
The entire source code of this article can be downloaded over here.
This article has been editorially reviewed by Suprotim Agarwal.
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 Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
ShobanKumar is an ex-Microsoft MVP in SharePoint who currently works as a SharePoint Consultant. You can read more about his projects at
http://shobankumar.com. You can also follow him on twitter @
shobankr