In this tutorial, you will learn how to build a new Episerver website via code. Whenever I'm starting a new project with a new team, before the heavy-duty coding can begin, a decision needs to be made in terms of the team working together... this decision seems to cause a surprising amount of controversy... use a shared database, create code that builds the pages for you, try and use the import/export feature. Depending on the client, using a shared-database is not always possible (or wanted).

In these situations, it's not feasible to constantly send database updates to each other on a daily basis, so one option is to write some code that builds the whole website up for you.  

If you have ever tried this with Episerver from a clean database, creating the pages is enough but when your website loads you will get a 500 error, as the homepage hasn't been set, the site hasn't been configured in Episerver and no hostnames have been added so you'll also get a license issue.

The Site Definition Repository

In order to do some of this cool configuration, you'll need to use the SiteDefinitionRepository.  As Epi is pretty good with dependency injection and working with interfaces, you can use ISiteDefinitionRepository, like so:

Before we get into the code, I'll cover one of the more useful methods within 'ISiteDefinitionRepository'.  If you simply want to check what existing sites have been set-up, you can use the List() method.

If you simply want to find out in your code what the current definition is, then you can use the SiteDefinition instance, like so:

How To Add A New Episerver Site Setting Via the API

In order to create a new setting you will need some information/access to certain properties:

  • Reference to the homepage object
  • The Site Url (needs to be a valid URI so include http:)
  • The Name you want to associate your website within Episever
  • The IIS hostnames that you want to associate with the site

The Code

To create a new setting you can use this method:

Creating a new site definition is pretty easy with the right know-how.  Use the 'ISiteDefinitionRepository', create a new site definition and then use the Save() method to update Episerver.