How to access Outlook and post to a blog using C#

Posting to your blog

Obviously everyone is using different software to manage their blog.  I can't give an example of every single method, however the simplest from a programmers perspective is if you can access the database of your blog via a webservice, and this is what we'll deal with here. For a full tutorial on building web services in .NET, check out this article.

Choose to add a new webservice to your site, or create an entirely independant project and call it something suitable - ours will be blog because it's an example. Firstly, you will need to add a few more items to your using list, so you can do XML serialisation of structures and objects.  I will assume your database is MS SQL Server too, so ensure the following are listed in addition to the defaults for a webservice:

using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Common;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.Xml.Serialization;

You want to be as object oriented as possible when building your webservice, so you should define a NewsItem structure to pass back and forth, you can adjust this to include whatever you need to store in an article:

public struct NewsItem
{
    public int id;
    public string topic;
    public string subject;
    public string postedby;
    public DateTime postedate;
    public string content;
}

This will allow you to reference rows in your database as objects, a simple organisational benefit that crosses over and permits easy use of methods of the webservice without passing a lot of parameters.  It also allows you to add groups of entries to an ArrayList, which is a big benefit (although there is a problem converting from an object transferred by a webservice and an ArrayList, if you ever do this you will need to iterate through the object and add the entries back to an ArrayList - .NET does not support converting from an object[] to an ArrayList).

You can then build your method for adding the article to the database.  I have used the database on my blog as an example, you will obviously need to change the insert statement and connection string to fit your situation.  There is also no exception handling, ideally you should enclose the opening of the connection and the executing of the query in try...catch blocks.

Note that the XmlInclude for the NewsItem struct is listed, this allows the webservice to accept a newsitem given as a parameter - otherwise if would not know to serialise the structure.

[WebMethod]
[XmlInclude(typeof(NewsItem))]
public void AddArticle(NewsItem newarticle)
{
    SqlConnection sqlcn = new SqlConnection("Data Source=(local);" +
        "Initial Catalog=NullifyDB;" +
        "Integrated Security=SSPI");
    sqlcn.Open();
    SqlCommand sqlcmd = new SqlCommand("INSERT INTO newsarticle (subject, topic, content, uid) VALUES (@subject, @topic, @content, @postedby);", sqlcn);
    sqlcmd.Parameters.Add("@subject", newarticle.subject);
    sqlcmd.Parameters.Add("@topic", newarticle.topic);
    sqlcmd.Parameters.Add("@content", newarticle.content);
    sqlcmd.Parameters.Add("@postedby", 253);
    sqlcmd.ExecuteNonQuery();
    sqlcn.Close();
}

You should then provide additional methods for anything else you would want to do, such as listing articles, deleting articles, and editing.  For Outlook integration you really only need this method.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow