Using .NET to Post a New BlogPost in Wordpress

Posted by: Shoban Kumar , on 11/22/2009, in Category WinForms & WinRT
Views: 195425
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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 4, 2010 7:19 AM
Is there any sample for web? in .net
Comment posted by Naveen Kumar on Tuesday, January 5, 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 5, 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
Comment posted by kolza on Friday, August 6, 2010 9:00 AM
"   
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 "
Comment posted by kenzo on Friday, August 20, 2010 4:12 AM
I've studied C# a long time ago. This somehow refreshes my memory. I'm starting my blogpost http://evoire.com/members/shockware2/ about my work. tell me guys what you think. thanks!
Comment posted by AffPortal on Wednesday, September 22, 2010 2:28 PM
Hey man, big thanks for posting this. I am writing a UI for a blog link network for my members site, www.affportal.com and this code is exactly what I was looking for. Thanks for making my job one heck of a lot easier.

~ Corey
Comment posted by dedeblk on Wednesday, November 3, 2010 12:31 AM
hi, nice post.
btw how to add custom value
i tried with :
Public custom_fields As String
...
Dim custom() As String = {"thumbs", "imgurl"}
newBlogPost.custom_fields

but didn't work> any idea?
Comment posted by dedeblk on Wednesday, November 3, 2010 12:33 AM
hi, nice post.
btw how to add custom value
i tried with :
Public custom_fields() As String
...
Dim custom() As String = {"thumbs", "imgurl"}
newBlogPost.custom_fields = custom

but didn't work, any idea?
Comment posted by Ishthiyaque Ahmed on Monday, December 20, 2010 5:43 AM
What is Categories,ClientProtocol? are they variables? of what type?
Comment posted by cialis on Thursday, June 16, 2011 4:35 AM
nice post thanks for sharing.

http://www.medicinaligenerici.com/
Comment posted by Parveen on Tuesday, July 12, 2011 7:06 AM
Can you please suggest or provide a sample code to post blog when the xml-rpc is disable. Actually my requirement is to post blog on other sites when the xml-rpc is disable. Thanks in advance...
Comment posted by Sarvesh on Friday, September 9, 2011 6:36 AM
Hi,
I am getting "Method Not Allowed" error while posting the blog in wordpress.
Comment posted by adam on Wednesday, November 16, 2011 7:49 AM
hi thank you for your post. the code works perfectly but do you know how to create custom fields with c#?
Comment posted by Bibhu Dutta on Thursday, November 17, 2011 3:48 AM
Hi, I am getting "Method not allowed" exception.
Comment posted by John on Tuesday, January 24, 2012 4:51 PM
I get a fault Exception [500] Content,Title and Excerpt are empty error...any suggestions?
Comment posted by John on Tuesday, March 13, 2012 12:19 AM
Anything?
Comment posted by tori on Saturday, May 26, 2012 12:43 PM
this code does not work.

and it posts only to the default uncategorized category. i tried to change post id but it still posts to the same default category.

you even miss :

Public Structure category
        Public categoryId As Object
        Public parentId As Object
        Public description As Object
        Public categoryName As Object
        Public htmlUrl As Object
        Public rssUrl As Object
    End Structure
Comment posted by Akash on Monday, June 11, 2012 3:08 AM
Hey there,
Thanx for the code explanation,
but how would i get my "blog id".??
Comment posted by Valiant Systems on Wednesday, June 13, 2012 6:22 AM
For posting the values with categories
----------------------------------------
In Button Click ()
{
string[] cats = {"News","Movies"};
blogpost newpost = default(blogpost);
newpost.title = "Some value";
newpost.description = "Some Value";
newpost.categories = cats;
var categorie = (IgetCatList)XmlRpcProxyGen.Create(typeof(IgetCatList));
var clientprotocal = (XmlRpcClientProtocol)categorie;
clientprotocal.Url = "http://i9live.com/xmlrpc.php";
string result = null;
result = categorie.newPage(1, "admin", "valiant123", newpost, 1);
}
In struct public struct blogpost
{
    [CookComputing.XmlRpc.XmlRpcMember]
    public string title;
    [CookComputing.XmlRpc.XmlRpcMember]
    public string description;
    [CookComputing.XmlRpc.XmlRpcMember]
    public string[] categories;
}  

Comment posted by batori on Tuesday, July 10, 2012 4:49 PM
I did test it, it wont work, it always posts to default category. Im using latest wp version.

This is my code :


Dim newBlogPost As blogInfo = Nothing
        newBlogPost.title = "test"
        newBlogPost.description = "test"
        newBlogPost.category = "movies"
            Dim categorie = DirectCast(XmlRpcProxyGen.Create(GetType(IgetCatList)), IgetCatList)
            Dim clientprotocal = DirectCast(categorie, XmlRpcClientProtocol)
        clientprotocal.Url = "http://site.org/xmlrpc.php"
            Dim result As String = Nothing
        result = categorie.NewPage(1, "test", "testtest", newBlogPost, 1)
Comment posted by Divya on Monday, August 13, 2012 5:29 AM
how can i edit post in word press?
can any one share source code for
metaWeblog.editPost (postid, username, password, struct, publish) returns true
Comment posted by Selin on Saturday, November 3, 2012 12:48 AM
Thank you for your article. It was great finding this cıde. I have been trying for a month to send an article from c# to wordpress with webbrowser. Then I met your article and i learnt about xml-rpc library.
Comment posted by Jerz on Tuesday, November 13, 2012 10:32 PM
Does this work in Visual Basic Express 2010?
Comment posted by asway on Wednesday, December 19, 2012 6:12 PM
how to attach image with .NET VB?
Comment posted by David on Friday, January 4, 2013 1:54 PM
This is not working for me at all, I even copied the instructions step by step several times and has a whole lot of errors.
Comment posted by michael clood on Sunday, January 6, 2013 2:37 PM
how to set another category for new post?
Comment posted by dusijun on Thursday, March 28, 2013 4:50 AM
Thanks, test your code. It works.
Comment posted by Tonino on Thursday, April 11, 2013 4:09 PM
Thanks.
VB
I modified Structure
Public Structure blogInfo
        Public title As String
        Public description As String
        Public categories() As String
End Structure

...and, for example for post in "Download" category:

Dim cats() As String = {"Download"}
newBlogPost.categories = cats
Comment posted by Nitin on Thursday, January 30, 2014 6:52 AM
Hi Can you please guide me to how to post it without using XML-RPC.
Comment posted by Nazim on Sunday, March 2, 2014 10:09 AM
Hello.  how to send image as "set thumbnail image" to wordpress?
Comment posted by Sakshi on Friday, September 5, 2014 4:31 AM
Hi
can some one please guide on how to add comments using xmlrpc.net.
Comment posted by Satish Lalam on Tuesday, September 23, 2014 1:06 AM
This code does not work.
Comment posted by Satish Lalam on Tuesday, September 23, 2014 1:08 AM
Please tell me How to Post the Article in Wordpress Blog??
Comment posted by Peter on Tuesday, October 21, 2014 1:22 PM
How is it possible to attach an image with the post?
Comment posted by Vijay on Monday, December 15, 2014 4:23 AM
Hi Shoban,
Will it work for Wordpress self hosted site?