In this tutorial, you will learn how to install a new Episerver database just using the power of 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. This decision seems to cause a surprising amount of controversy... how will the development process work. The main challenge of getting a large CMS team working together is content. As new content within the CMS is created, how do other team members access it? Does each team member have a local database? If you have a team of 10 people, having 10 people trying to keep their Episerver databases synced will waste a lot of time. To get around this issue there are a few options. Will the team use a shared database, will you write some code to add new content, will you use the import/export feature?

A lot of teams use a shared database. Depending on the client, using a shared database is not always possible (or wanted). It's not feasible to constantly do database exports and shared them around. So what do you do? Trying to populate all the content in an Episerver website using code is possible. In this approach, your start with a clean database and write code to create pages and blocks. When someone pulls the code they get the content updated automatically. The issue of starting from a blank slate on a daily basis is that when your website loads you will get a 500 error, as the homepage won't have been set. In Episerver, we need to set the site definition or the site will not work. If you start from a blank slate all the time, no hostnames will have been added so you'll also get a license issue. Manually updating site definitions each time you sync data is painful. Solving this last problem is possible in code though, read on to learn how!

The Site Definition Repository

In order to do some of this cool configuration, you'll need to use the SiteDefinitionRepository. Episerver is pretty good with dependency injection and working against interfaces. This means you can access 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 wanted to find out in code what the current site definition is, you can use this code:

How To Add A New Episerver Site Definition Entry In Code

In order to create a new site definition setting you will need access to this data:

  • Reference to the start page
  • The Site Url (needs to be a valid URI so include HTTPS/HTTP)
  • The name that you want the site definition to display within your website
  • The IIS hostnames that you want to associate with the site

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 use the Save() method to update Episerver. Happy Coding 🤘