Kentico CMS 6.0 Developer's Guide

Creating a new website

Creating a new website

Previous topic Next topic Mail us feedback on this topic!  

Creating a new website

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

The following example shows how you can create a new site based on the Blank website template:

 

[C#]

 

using CMS.SettingsProvider;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.SiteProvider;
using CMS.CMSImportExport;

 

...

 
    // Site name
    string siteName = "testAPIsite";
 
    try
    {
        // Create site import settings
        SiteImportSettings settings = new SiteImportSettings();
 
        //Initialize the settings
        settings.SiteName = siteName;
        settings.SiteDisplayName = "Test API site";
        settings.SiteDescription = "Site for testing the API examples";
        settings.SiteDomain = "127.0.0.254";

 
      // Get 'Blank site' web template
      WebTemplateInfo template = WebTemplateInfoProvider.GetWebTemplateInfo("BlankSite");

 
        // Template exists
        if (template != null)
        {

          // Set source file path
          string templatePath = template.WebTemplateFileName;
           settings.SourceFilePath = Server.MapPath(templatePath.TrimEnd('\\') + "\\");

 
            // Load default selection to preselect the objects
            settings.LoadDefaultSelection();
 
            // Create new site using 'Blank' template
            ImportProvider.ImportObjectsData(settings);
 
            // Run site
            SiteInfoProvider.RunSite(siteName);

 
          this.lblInfo.Text = string.Format("New site with code name '{0}' has been created.", siteName);

            return;
        }
        else
        {

          this.lblInfo.Text = string.Format("Failed to create new site '{0}'.<br />Web template 'Blank site' doesn't exist.", siteName);

            this.lblInfo.CssClass = "ErrorLabel";
        }
    }
    catch (RunningSiteException)
    {

      this.lblInfo.Text = string.Format("Failed to run site '{0}'.", siteName);

        this.lblInfo.CssClass = "ErrorLabel";
    }
    catch (Exception ex)
    {

      this.lblInfo.Text = string.Format("Failed to create new site '{0}'.<br />Original exception: " + ex.Message, siteName);

        this.lblInfo.CssClass = "ErrorLabel";
    }