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!

Tuesday 18 January 2011

Exceptioneer - superb and a doddle to integrate into N2

One of the lads at work pointed me in the direction of Exceptioneer which quite honestly is fantastic.

Although Elmah which comes with N2 is simple to use and is very helpful in tracking bugs, if you get an exception on one of your N2 sites which is "buried down" under a load of code it's not easy to find. Exceptioneer gives you information on the current username, stack trace, where it was called from on the front end etc. There's simply so much information it collects for you that it would be hard to do a screenshot.

Another big bonus is that it also tracks your javascript errors! I can't recommend this enough and this will definitely be a part of all N2 work I do from now on... not that I write code with loads of exceptions in it of course! :)

Finally, if all that wasn't good enough... Exceptioneer has a free account which can log up to 2000 exceptions a month.

Not strictly N2, but a couple of fantastic tools

I'm currently working on a project of my own to convert an existing local community site into a full-on N2 site. The main part has involved migrating the message forum, news gathering service and other migration coding.

I'm wanting to post the news regularly to Twitter and, once it's up and running, Facebook. I've found this library as a superb, simple way to post to Twitter now they are using oAuth which is more complicated than their old authentication method. Check this out:

www.diplo.co.uk/blog/2010/8/9/oauth-with-twitter.aspx