How To Install 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 share database is not always possible (or wanted).

In these situations, it's not feasable 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.  However, 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.  Fear not, you can get around all of this and in today's tutorial I'm going to show you how to do that.

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:

     var siteDefinitionRepository = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>();

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:

   var currentDefinition = SiteDefinition.Current;
     var definitions = siteDefinitionRepository.List();

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:

        public void CreateSite(IContent homePage, string siteUrl)
        {
            var siteDefinitionRepository = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>();
            if (siteDefinitionRepository.List().Any())
                    return true;

            var siteDefinition = new SiteDefinition
            {
                Name = "TSC.Website",
                SiteUrl = new Uri(siteUrl),
                StartPage = homePage.ContentLink,
                Hosts = new EditableList { new HostDefinition { Name = "*", Language = new CultureInfo("en") }
            };

            siteDefinitionRepository.Save(siteDefinition);
            return true;
        }

Conclusion

As you can see, creating a new site definition is pretty easy with the right know how.  Use the ISiteDefinitionRepository, create a new SiteDefinition and then use the Save() method to update Episerver.  

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top