Integrating N2CMS into Who Can Help Me?
Tags: MVC, N2 CMS, Who Can Help Me February 3rd, 2010This is going to be the first of three or four posts around integrating the .Net open source content management system N2CMS into the Sharp Architecture demo application Who Can Help Me?. Who Can Help Me? was born out of our work on Fancy Dress Outfitters, which had N2CMS at its core, however we decided to leave the CMS piece out of WCHM as the application didn’t really need it and we didn’t want to overly complicate things. WCHM already has quite a lot going on – even though it’s a relatively simple application, the idea was to present an architecture reference for an application that would scale and potentially become a lot more complicated.
However, over the last few weeks, a number of people have asked about the approach we took with N2 and ASP.Net MVC on Fancy Dress Outfitters and how we would go about adding N2CMS to WCHM. So, after a brief discussion with the guys, we decided it would be beneficial to create a new branch of WCHM that is backed by N2CMS.
I want these posts to walk through the steps taken and changes made to the solution in order to integrate the CMS. I’m going to write the posts as I’m making the changes, so ideally I’ll be able to link to specific changesets that I check into the branch.
Step 1 – Adding the N2 assemblies
I already had a download of the N2CMS trunk code from here as of a week or two ago as I’m using it again on my current project. I built the solution using the Deploy_Everything-vs2008.bat script, after replacing the System.Data.SQLite.dll for a 64 bit version.
This creates the output folder and deploys the example, templates and edit sites with all the necessary references. I grabbed the necessary dlls and checked them into the ReferencedAssemblies folder under the N2CMS folder.
Step 2 – Add edit site to web project
I then grabbed the deployed edit site (which is the CMS admin interface) from under one of the template sites in the output folder and added it to a new Edit directory under the WhoCanHelpMe.Web MVC web application project. The edit site is a Web Forms project, however they sit nicely side by side together as when browsing to the .aspx pages of the Web Forms project, the MVC routing knows that it should ignore real existing file requests and so they just get processed as normal.
Another option is to split out the edit site and the public facing website into two separate web projects. This is a good idea as it allows you to deploy the two sites differently. For example, you could set up two separate websites in IIS:
and create a virtual directory to the edit site in only the second site:
http://admin.yoursite.com/edit
which is routed to the edit site location. By IP restricting the admin.yoursite.com site in IIS, you are preventing unwanted users from even being able to browse to your edit site url, before the even get chance to log in.
However, this seemed like overkill for the WCHM demo, so I took the easy route and just dropped the edit site into the existing web project.
Step 3 – Creating initial content definitions
N2 requires a root node and a start node as an initial site skeleton. Each node must be of a particular content type, so we need to start by creating these type definitions in code so that we can set the root and start nodes in both the database and the N2 config section in web.config.
I created two definitions – a SiteRoot:
namespace WhoCanHelpMe.Domain.Cms.Pages
{
using N2;
using N2.Details;
using N2.Installation;
using N2.Integrity;
/// <summary>
/// The site root definition
/// </summary>
[PageDefinition("Site Root",
Description = "A site root used to organize home pages.",
SortOrder = 0,
InstallerVisibility = InstallerHint.PreferredRootPage,
IconUrl = "~/edit/img/ico/png/page_gear.png")]
[WithEditableTitle("Title", 5, Focus = true, ContainerName = Tabs.Content)]
[RestrictParents(AllowedTypes.None)]
public class SiteRoot : AbstractPage
{
/// <summary>
/// Gets site root Url.
/// </summary>
/// <value>
/// The site root url.
/// </value>
public override string Url
{
get { return "~/"; }
}
}
}
And a HomePage:
namespace WhoCanHelpMe.Domain.Cms.Pages
{
#region Using Directives
using N2;
using N2.Details;
using N2.Installation;
using N2.Integrity;
using N2.Web.UI;
#endregion
/// <summary>
/// The home page definition.
/// </summary>
[PageDefinition("Home Page",
Description = "A home page template.",
SortOrder = 440,
InstallerVisibility = InstallerHint.PreferredRootPage | InstallerHint.PreferredStartPage,
IconUrl = "~/edit/img/ico/png/page_world.png")]
[WithEditableTitle("Title", 5, Focus = true, ContainerName = Tabs.Content)]
[RestrictParents(typeof(SiteRoot))]
public class HomePage : AbstractPage
{
}
}
Both these definitions inherit from a base AbstractPage:
namespace WhoCanHelpMe.Domain.Cms.Pages
{
#region Using Directives
using System.Collections.Generic;
using System.Security.Principal;
using N2;
using N2.Collections;
using N2.Web.UI;
#endregion
/// <summary>
/// A base class for page items.
/// </summary>
[TabContainer(Tabs.Content, "Content", 0)]
public abstract class AbstractPage : ContentItem, IItemContainer
{
#region IItemContainer Members
/// <summary>
/// Gets the item associated with the item container.
/// </summary>
public ContentItem CurrentItem
{
get { return this; }
}
#endregion
/// <summary>
/// Overridden for performance reasons
/// to save N2 having to do role based
/// authorization look ups for each page
/// </summary>
/// <param name="user">
/// The current user.
/// </param>
/// <returns>
/// Always true
/// </returns>
public override bool IsAuthorized(IPrincipal user)
{
return true;
}
/// <summary>
/// Gets all children of the specified type
/// of the current item
/// </summary>
/// <typeparam name="T">
/// The type filter
/// </typeparam>
/// <returns>
/// The list of children
/// </returns>
public virtual IList<T> GetChildren<T>() where T : ContentItem
{
return new ItemList<T>(
Children,
new TypeFilter(typeof(T)));
}
}
}
Step 4 – Create database schema
I then added the N2CMS database schema to the solution. As we’re already using the Visual Studio Team System Database Edition, I was able to create a database project easily by pointing the wizard at an existing installation of N2 that I already had. The database can then easily be created by right clicking the database project and selecting deploy.
Without the database project, you’d need to create the database schema manually by running the scripts that N2 provide as part of the download.
Once your database is created, remember to grant the correct permissions to be able to access it (This had me banging my head against the wall for a while!).
Next
That’s enough for now, we don’t have a working site just yet and there’s still a lot to do, but I want to break this up into manageable chunks and also am writing this as I go along. Next we need to add in our root nodes to the database, register our N2 routes and start to build out the controllers and content definitions….

February 4th, 2010 at 1:48 pm
[...] the first post [...]
February 4th, 2010 at 4:09 pm
Thanks James, looking forward to reading next parts soon.
February 6th, 2010 at 11:11 pm
Awesome stuff, James – can’t wait to read the rest.
February 12th, 2010 at 5:11 pm
[...] the first and second post [...]
February 28th, 2010 at 6:30 pm
[...] the first, second and third posts [...]
August 20th, 2010 at 10:01 pm
Where do you place the pages.
I the templates project, there is a templates direcotory.
However if you want to start from scratch with the simple c# or Vb versions, there is no templages directory, just a models and ui
I am very interested in being abole to create a custom N2 website, but having a hard time starting. (I was able to generate a new page template using the templates site, but I would really like to build from the ground up.
Thanks in advance
Rob
PS I didn’t find the n2 forum to be too helpfull and the hosting group i use doens’t support MVC.
September 26th, 2010 at 5:07 pm
Hey very good weblog!! Man .. Beautiful .. Wonderful .. I will bookmark your weblog and take the feeds also…
March 3rd, 2011 at 5:38 pm
all new info,much appreciated,keep it comin’
September 9th, 2011 at 4:30 pm
It’s like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is great blog. A great read. I will certainly be back.
February 26th, 2012 at 2:15 pm
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates
xookz.com Free Templates