Scott Hanselman

Migrating Content from Random Blogs to DasBlog

September 23, '05 Comments [9] Posted in ASP.NET | DasBlog | XML
Sponsored By

This guy Ernie emailed me saying that he had some random, possibly home-grown, blog with content in Access that he wanted to migrate to his new DasBlog. I don't ordinarily do this (and don't plan to ever again) but it's so easy to move content into DasBlog that I decided to take this opportunity to write an example I could point other folks to.

Note: this is just an example that converted Ernie's blog. It's not something that will work unchanged for you, so don't ask. :) Ernie had a "news" table and a "comments" table. Blog posts were in news and used "blogid" as a unique id.

Additional note: Often you can use the same unique ids in DasBlog. You don't need you use a GUID for the DasBlog EntryId. In this case, Ernie's BlogID was just an int and it was unique enough. He could implement redirects if someone visited /myoldblog.asp?id=4 to /mynewdasblog/permalink.aspx?guid=4 and all his old content would get reindexed by Google and none of the old links would be invalid.

Just make a Console application in C# or VB.NET and add a reference to newtelligence.DasBlog.Runtime. I used a DataReader and just hard-coded the indexes of each column. It's not like this will ever be run again. It takes a little while to run because the DasBlog engine expects to run in the context of ASP.NET so it isn't able to use the ASP.NET Cache. The performance degrades O^n when run locally as your content folder grows. If that becomes a problem, run your converter in the context of IIS/ASP.NET and you'll get the high speed access.

In this example, the DasBlog dayentry.xml and dayfeedback.xml files will be left in the directory that the application was run in. I also converted newlines to <br> and when a blog post didn't include a title, I used the first 20 characters of the post as the title.

This took about 15 mins at lunch, so you really can transfer all your existing blog's data to DasBlog quickly, usually using just the Entry class and the Comment class. Note how they relate to each other using the EntryId as the key.

static void Main(string[] args)
{
    IBlogDataService dataService = 
BlogDataServiceFactory.GetService(AppDomain.CurrentDomain.BaseDirectory,null);
    string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;
Data Source=ernie.mdb;Mode=Share Deny None";
    using(OleDbConnection conn = new OleDbConnection(connStr))
    {
        conn.Open();
        using(OleDbCommand newsCmd = new OleDbCommand("select * from News",conn))
        {
            using (OleDbDataReader reader = newsCmd.ExecuteReader())
            {
                while(reader.Read())
                {
                    int blogId = reader.GetInt32(0);
                    DateTime date = reader.GetDateTime(1);
                    DateTime time = reader.GetDateTime(3);
                    DateTime correctDate = 
new DateTime(date.Year,date.Month,date.Day,time.Hour,time.Minute,time.Second);
                    string blogText = reader.GetString(2);
                    string blogTitle = reader.IsDBNull(4) ? String.Empty : reader.GetString(4);
 
                    Entry entry = new Entry();
                    entry.CreatedLocalTime = correctDate;
                    entry.ModifiedLocalTime = correctDate;
                    entry.Title = 
(blogTitle.Length > 0 ? blogTitle :
blogText.Substring(0,Math.Min(20,blogText.Length)));
                    entry.Content = blogText.Replace("\r\n","<br>");
                    entry.EntryId = blogId.ToString();
                    entry.Categories = "main;old site";
                    entry.Author = "Ernie";
                    dataService.SaveEntry(entry);
                }
            }
        }
 
        using(OleDbCommand newsCmd = new OleDbCommand("select * from comments",conn))
        {
            using (OleDbDataReader reader = newsCmd.ExecuteReader())
            {
                while(reader.Read())
                {
                    int blogId = reader.GetInt32(1);
                    DateTime date = reader.GetDateTime(4);
                    string commentText = reader.GetString(2);
                    string commentName = reader.GetString(3);
 
                    Comment comment = new Comment();
                    comment.CreatedLocalTime = date;
                    comment.ModifiedLocalTime = date;
                    comment.TargetEntryId = blogId.ToString();
                    comment.Author = commentName;
                    comment.Content = commentText;
                    dataService.AddComment(comment);
                }
            }
        }
    }

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Monday, 26 September 2005 13:03:03 UTC
How to move from blogger to dasblog ?
Tuesday, 27 September 2005 00:03:45 UTC
As much as I despise the language, this might be a perfect example of an instance where perl would be appropriate. Some cheesy little script that will never, ever be run again.

Of course, those things have a way of sticking around, so python still seems like a better choice. C# is just overkill.
Tuesday, 27 September 2005 03:43:32 UTC
Hm...I hear what you're saying kind of Terry, but "overkill?" Um, how do you figure? That's like 25 lines...not sure how that's heavy or overkill.
Scott Hanselman
Tuesday, 27 September 2005 03:46:57 UTC
Andrei, to export, do this: http://help.blogger.com/bin/answer.py?answer=130&topic=12 to your blogger account, then write a small program to transform this format into the DasBlog format. If you do this, send me the resulting export file and I'll take a look.
Scott Hanselman
Wednesday, 28 September 2005 04:01:42 UTC
Andrei: I followed the same pattern above to make a GUI for porting Blogger content over to das blog. You can find it on my blog.
Thursday, 17 November 2005 03:16:01 UTC
flippin' awesome. This is the clincher. I took about 20 minutes modifying the code and voila, all my content from my circa 2002 ASP/Access CMS gets imported into das blog. I am in the process of porting my site and will have it running on DasBlog by next Monday.

Saweet, thanks Scott.
Thursday, 22 December 2005 00:23:51 UTC
I did the conversion, yet when I put the resulting xml files in the /content directory, they aren't displayed. What else do I need to do?

Nick
Thursday, 22 December 2005 00:31:25 UTC
Ahh, restarting IIS appears to have done the trick
Monday, 02 January 2006 14:46:11 UTC
I modified your code to move my blog over to dasblog (it was previously .Text and the alternatives seemed to be overkill and not as functional). I put the resulting files in my /content directory and asked my sharedhosting admin to reset IIS, the old posts are still not visible. Any ideas?
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.