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);
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!
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"
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) ;)
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.
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);
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.
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,