Creating a new web site

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

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.GlobalHelper;

using CMS.SiteProvider;

using CMS.ImportExport;

 

...

 

       // Site name

       string siteName = "testAPIsite";

 

       try

       {

           // Create site import settings

           SiteImportSettings settings = new SiteImportSettings();

 

           //Initialize the settings

           settings.NewSiteName = siteName;

           settings.NewSiteDisplayName = "Test API site";

           settings.NewSiteDescription = "Site for testing the API examples";

           settings.NewSiteDomain = "127.0.0.254";

 

           // Get 'Blank site' web template

           WebTemplateInfo template = WebTemplateInfoProvider.GetWebTemplateInfo("Blank site");

 

           // Check if the template exists

           if (template != null)

           {

               // Set source file path

               string templatePath = template.WebTemplateFileName;

               settings.SourceFilePath = templatePath.TrimEnd('/') + "/";

 

               // Create new site using 'Blank' template

               ImportProvider.ImportSite(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 = "Failed to create new site 'testAPIsite'.<br />Web template 'Blank site' doesn't exist.";

           }

       }

       catch (RunningSiteException)

       {

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

       }

       catch (Exception ex)

       {

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

       }