Saturday, 29 January 2011

Importing data into N2

One of the projects I'm working on at the moment is for a local community website. The old site has a message forum and we want to migrate the content into N2 preserving as much of the data as we can.

It is quite easy to do with N2 in that you can create a class of any page, populate it and save it.

Here's an example from the code which imports the actual messages on the message board - it's not the best, but it works and demonstrates the principle.


                N2.Templates.Mvc.Models.Pages.MessagePage mp = new Models.Pages.MessagePage();
                mp.Created = reader.GetDateTime(reader.GetOrdinal("datePosted"));
                mp.Parent = parent;
                mp.Author = reader.GetString(reader.GetOrdinal("MessageAuthor"));
                mp.Text = reader.GetString(reader.GetOrdinal("MessageText"));

                mp.Title = string.Format("Reply by {0}-{1}", reader.GetString(reader.GetOrdinal("Username")), ForumHelper.GetDateAsNumbers(mp.Created));


                N2.Context.Persister.Save(mp);
                N2.Context.Persister.Save(parent);


There's a couple of things to note. The title must be unique in N2 so I have got around this by creating the title as normal, but adding the date of when it was created onto the end. You must also specify the parent page too and save it after you have created the message to create the relationship between them.

What I am impressed with is that I can take an existing message board and import all the forums, topics and messages and do so really easily with N2. I have also imported all the user account information, profiles etc as well and we're hoping to get the new site live within a month. I'll let you know how we go on!

2 comments:

  1. Hey man! Keep posting those cool tutorials

    ReplyDelete
  2. Really informative blog with it's authentic source of content. Assignment Help Writers

    ReplyDelete