[eng] BlogEngine multibloging made easy!

While I was on New Zealand last year, I wondered what I would take to make a BlogEnginge "multiblog" application. I spoke to Mads about the issue, hoping he had already been implementing that feature somewhere under the covers of BE, but unfortuneatly I came to early.

He told me that one of the developers on the famouse piece of software actually was working on such a feature, but only in his sparetime. So I contacted the developer for more details. His name is Roman D. Clarkson and has been working on BE for quite some time. Roman told me about his implementation and I went off, trying to run and test it, all while he was developing it. Hours, days and weeks went by. Eventually I realised that my issue was bigger than I had first expected, and all of a sudden I felt like one of those customers that order a feature but doesn't realize how much time it will take. Doooh!

The multiblog application came to my mind when I was preparing ActiveDeveloper.dk for handling more blogs. I felt crippled because I had to log on to the server, configure a virtual directory and move files each time a new blog had to be created. Booooring work!

I didn't want to change the blogging platform to something else, because I believe in supporting a local project, and because there is no blogging software that is as good as BlogEngine.

On Friday night I had been over at ZYB to drink a cold beer with Mads. The guys over there are seriously having fun!!! We didn't talk about the multiblog feature at all, because the application left my mind months ago. Instead we talked about who is going to TechEd EMEA and who we would like to meet down there.

I left ZYB early and went home to watch a movie. I accidently turned on my computer while sitting in my couch and thought "ohh well, I guess I could fire up Visual Studio and build something". So I did.

A couple of days ago I had fallen in love with the IIS 7 API, so the first thing I did was to include the Microsoft.Web.Administration.dll file in the new project. But I also have a lot of love for BlogEngine. All of the sudden I realised that my "multiblog" application could become true, or at least kind of :)

I added a IIS web site called MultiBlogs and applied my desired configuration to it.

IISsite

I configured the config directory under "%SystemDir%\Windows\System32\inetsrv\config" and had to add the IIS_USERS to the user list and give it sufficient rights.

applySecurity

If you want to read or write to the files withn the config folder, from a website, you need to add this group.

Then I downloaded the lateset version of BlogEngine and created a directory where I copied a all the BlogEngine files into. I named this directory "BlogFiles".

BlogFiles

So now I was ready to get "code cracker lacking". So I opened up the MultiBlogs website from Visual Studio.

I created a simple page where I added a few controls.

<form id="form1" runat="server">
<div>
    What is my name ?
    <asp:TextBox ID="tbBlogName" runat="server" />
    <asp:RequiredFieldValidator ID="rfvBlogName" ControlToValidate="tbBlogName" ErrorMessage="*"
        runat="server" />
    <asp:Button ID="Button1" Text="Fyyyyr" runat="server" OnClick="Unnamed1_Click" />
    <br />
    <asp:Label ID="lblMessage" runat="server" />
</div>
</form>

I added 2 simple classes in my App_Code directory. BlogInstaller.cs and IISHandler.cs.

I wanted to use the BlogInstaller class for creating the new blog directory on the disk, but also for the copying of files. The files that should be copied from the "BlogFiles" directory into the new directory. So 2 methods and a construtor - easy money!

IISHandler.cs, I would this for 2 things as well. Check if an application already exists in the "MultiBlogs" website, but it would also be the creator for both an application and a virtual directory for the new blog on the IIS.

The code file for the aspx page ended up looking like this.

private const string IISSiteDirectoryPath = @"C:\WebSites\MultiBlogs\";
private const string BlogCleanInstallDirectoryPath = @"C:\WebSites\BlogFiles\BlogEngine.Web\";
private const string ApplicationPoolName = "MultiBlogs";

protected void Unnamed1_Click( object sender, EventArgs e )
{
    if( Page.IsValid ) {
        string blogName = tbBlogName.Text;

        foreach( var item in VirtualDirectoryCollection.InvalidVirtualDirectoryPathCharacters() ) {
            if( blogName.Contains( item ) ) {
                lblMessage.Text = String.Concat( item, " is not allowed!" );
                return;
            }
        }

        if( !IISHandler.ApplicationExists( IISSiteDirectoryPath, blogName, ApplicationPoolName ) ) {
            BlogInstaller installer = new BlogInstaller( String.Concat( IISSiteDirectoryPath, blogName ), new DirectoryInfo( BlogCleanInstallDirectoryPath ), new DirectoryInfo( IISSiteDirectoryPath + blogName ) );
            IISHandler.CreateVirtualDirectory( IISSiteDirectoryPath, blogName, ApplicationPoolName );

            lblMessage.Text = "Created!";
        } else {
            lblMessage.Text = "Exists!";
        }
    }
}

Now, it is possible for me to create blogs on the fly. Now I only need a site where I can install it...dohhhh!

Download the sourcecode here.