London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Creating a new site in multisite environment programmatically

Vote:
 

Hi,

 

Is there a way to create a new site and set the needed configuration properties programmatically?

In addition, is there a way to create block folders under global library folder programmatically?

 

 

Cheers,

#71405
May 17, 2013 6:10
Vote:
 

The short answer is yes and yes :-)

Perhaps the easiest way to create a site programatically is to use the helper classes in EPiServer.UI (that is what is used if you in admin mode creates a new site from "Site Information" under tab Config). The class to use is EPiServer.UI.Admin.SiteMgmt.SiteInformationHandler and method AddSiteSetting. If you do not want to take a dependency to EPiServer.UI you can work against configruation classes also.

To create a folder under global library you can do something like:

var contentFolder = Locate.ContentRepository().GetDefault<ContentFolder>(ContentReference.GlobalBlockFolder);
contentFolder.Name = "MyFolder";
Locate.ContentRepository().Save(contentFolder, SaveAction.Publish);

#71412
May 17, 2013 10:26
Vote:
 

Thanks for the info - i did find the SiteInformationHandler after reflecting a few dll's, but thank you anyhow!

Cheers for the block info aswell!

#71414
May 17, 2013 10:37
Vote:
 

One additional question regarding Site creation programmatically/manually:

Is there a way to define the siteDisplayName property programmatically/manuall that resides under episerver.config file? I am using the SiteInformationHandler.AddSiteSetting to create the site and it works perfectly. However i haven't been able to find a way to change the siteDisplayName of any site unless i change it manually from episerver.config. The property seems to be "inherited" based on the site you've logged in. Let's say that i have:

SiteID: "SiteA", siteDisplayName: "Name of site A"

SiteID: "SiteB", siteDisplayName: "Name of site B"
 

I log on to SiteA and create a new site called "SiteC". The siteDisplayName property will be "Name of site A".

 

Thanks in advance - I know that there is a way (crossing fingers) ;)

#71491
Edited, May 21, 2013 6:03
Vote:
 

Any possible updates on the latest question?

#71666
May 27, 2013 9:15
Vote:
 

To do this you need to work with the configuration classes. You can set the value on Settings configuration element and then persist the value with help of EPiServer.Framework.Configuration.GlobalConfigurationManager.

#71669
May 27, 2013 9:41
Vote:
 

You mean something like this?

// Add host setting 
                List<HostSetting> hostSettings = new List<HostSetting>();
                HostSetting hostSetting = new HostSetting();
                hostSetting.SiteHost = "http://" + siteUrl;
                hostSetting.Language = "fi";

                hostSettings.Add(hostSetting);

                // Create a new multisite to the current instance
                SiteInformationHandler.AddSiteSetting(siteName, "http://" + siteUrl, newSite.ID, hostSettings, false);
                //EPiServer.Configuration.Settings.MapUrlToSettings(new Uri("http://" + siteUrl)).SiteDisplayName = siteName;
                EPiServer.Configuration.Settings confSettings = EPiServer.Configuration.Settings.MapUrlToSettings(new Uri("http://" + siteUrl));
                confSettings.SiteDisplayName = siteName;
                confSettings.EnableSavingOfConfigurationToDatabase = true;

                System.Configuration.Configuration conf = confSettings.CurrentConfiguration;

                GlobalConfigurationManager.Save(conf, RestartMode.None);

    

#71674
May 27, 2013 11:12
Vote:
 

Hi Jonathan I tried the code you suggested but I get an error of  "The configuration file has been changed by another program." 

Seems the episerver.config file gets locked by the SiteInformationHandler.  It doesn't implement IDispose either so I haven't been able to get this working. 

#74010
Aug 16, 2013 23:55
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.