November Happy Hour will be moved to Thursday December 5th.

Set starting language and web sites on new EPiServer site

Vote:
 

Hi.

I've tried to find some documentation about this, but I can't find it.

Is it possible to:
1. set other languages as default instead of english and swedish?
2. set up web sites programatically?

// Anders

#88776
Jul 30, 2014 12:39
Vote:
 

Hi

From code can you can set which language branches that should be enabled. You do this through ILanguageBranchRepsoitory (e.g. through methods ListAll and Save).

You could create a site programatically like:

static ContentReference CreateStartPageAndSite()
        {
            var contentRepository = ServiceLocator.Current.GetInstance();
            var siteDefinitionRepository = ServiceLocator.Current.GetInstance();

            var startPage = contentRepository.GetDefault(SiteDefinition.Current.RootPage);
            startPage.Name = "Start";
            var startPageLink = contentRepository.Save(startPage, SaveAction.Publish, AccessLevel.NoAccess).ToReferenceWithoutVersion();

            var siteDefinition = new SiteDefinition
            {
                Name = "IntegrationTestSite",
                StartPage = startPageLink,
                SiteUrl = new Uri("http://localHost/")
            };
            siteDefinition.Hosts.Add(new HostDefinition { Name = siteDefinition.SiteUrl.Authority });
            siteDefinition.Hosts.Add(new HostDefinition { Name = SiteDefinition.WildcardHostName });
            siteDefinitionRepository.Save(siteDefinition);

            return startPageLink;
        }
#88780
Jul 30, 2014 14:24
Vote:
 

If you want to set a specific master language before a clean install, you could also tamper with the sql installer scripts:

http://hanskindberg.wordpress.com/2014/02/18/episerver-cms-7-5-svenska-as-master-language/

Typically you would do this if you require a specific master language in a "barebones" template project. 

#88781
Jul 30, 2014 14:46
Vote:
 

Hi Johan,

Thank you for you swift reply.

This seem to work, but I don't understand what you're doing when you're adding the WildcardHostName to the siteDefinition. What does this mean?

I managed to remove and add languagebranches and I'm currently testing it out.

I will post my results when I'm done.

EDIT: @Arild:

This is pretty cool. I will try this out. This is a good way to solve the problem. The only drawback is when you're working on multiple EPiServer sites at the same time. You will have to remember to change the script when you want something else.

// Anders

#88783
Edited, Jul 30, 2014 15:21
Vote:
 

@Arild:

This is pretty cool. I will try this out. This is a good way to solve the problem. The only drawback is when you're working on multiple EPiServer sites at the same time. You will have to remember to change the script when you want something else.

// Anders

#88784
Jul 30, 2014 15:25
Vote:
 

Since 7.5 is an EPiServer site multi-tenant, that means that several different sites can be hosted in the same IIS instance. Hence will e.g. ContentReference.StartPage be different depending on in which site context the request is in. How SiteDefinition.Current (which e.g. ContentReference.StartPage is dependent on) resolving work is that it will look if there is an HttpContext available, if so it will match the hostname from the request against configured hostnames for the sites to determine which site is to be considered Current.

However if there is no hostname match (or if HttpContext.Current is null) it will fallback to the site with a '*' (wildcard host) and take that as SiteDefinition.Current. So the shorter answer is add '*' host to the site you want none HttpContext calls to run under (e.g. scheduled jobs).

#88785
Jul 30, 2014 15:32
Vote:
 

Thank you for clarifying, Johan.

I've been playing around with the LanguagesBranches a little while now and what I'm trying to do is make norwegian the default language (though code).

The enabled property on the different branches doesn't have a setter, so do I need to loop through and first remove all the branches, then add norwegian back again with the enabled flag set to true?

Also... What happens if the WildcardHostName is not added to the sitedefinition? Will the scheduled job fail?

#88787
Jul 30, 2014 15:40
Vote:
 

Yes, I admit that it is a bit fainful to work with LanguageBranch... You could have code like:

var langBranch = langRepository.Load(CultureInfo.GetCultureInfo("en"));
                if (!langBranch.Enabled)
                {
                    LanguageBranch enabledBranch = new LanguageBranch(langBranch.ID, langBranch.LanguageID, langBranch.Name, langBranch.SortIndex, langBranch.RawIconPath, langBranch.URLSegment, true);
                    enabledBranch.ACL = langBranch.ACL.Copy();
                    langRepository.Save(enabledBranch);
                }

Regarding WildCardMapping when no wildcard host exist, then it is so that SiteDefinition.Current will always return an instance. However site specific settings like SiteUrl and StartPage will be null or ContentReference.Empty. So for a scheduled job it might be running fine, if however the job needs e.g. ContentReference.StartPage then it will fail.

#88788
Jul 30, 2014 15:50
Vote:
 

Thanks, Johan. That's just what I needed.

Here's my working code. This code disables english and swedish and enables norwegian. It also creates a StartPage and sets that page as the StartPage by defining up a new SiteDefinition:

var english = _languageBranchRepository.Service.Load(CultureInfo.GetCultureInfo("en"));
var swedish = _languageBranchRepository.Service.Load(CultureInfo.GetCultureInfo("sv"));
var norwegian = _languageBranchRepository.Service.Load(CultureInfo.GetCultureInfo("no"));

var englishDisabledBranch = new LanguageBranch(english.ID, english.LanguageID, english.Name, english.SortIndex, english.RawIconPath, english.URLSegment, false);
var swedishDisabledBranch = new LanguageBranch(swedish.ID, swedish.LanguageID, swedish.Name, swedish.SortIndex, swedish.RawIconPath, swedish.URLSegment, false);
var norwegianEnabledBranch = new LanguageBranch(norwegian.ID, norwegian.LanguageID, norwegian.Name, norwegian.SortIndex, norwegian.RawIconPath, norwegian.URLSegment, true);

englishDisabledBranch.ACL = english.ACL.Copy();
swedishDisabledBranch.ACL = swedish.ACL.Copy();
norwegianEnabledBranch.ACL = norwegian.ACL.Copy();

// Save norwegian as enabled because disabling both will cause an error because there will be no master language.
_languageBranchRepository.Service.Save(norwegianEnabledBranch);
_languageBranchRepository.Service.Save(englishDisabledBranch);
_languageBranchRepository.Service.Save(swedishDisabledBranch);
			
var startPage = _contentRepository.Service.GetDefault(SiteDefinition.Current.RootPage);
startPage.Name = "Hjem";
var startpagePageLink = _contentRepository.Service.Save(startPage, SaveAction.Publish, AccessLevel.NoAccess).ToReferenceWithoutVersion();

var siteDefinition = new SiteDefinition
{
	Name = "Website",
	StartPage = startpagePageLink,
	SiteUrl = new Uri("http://dev.website.no/")
};
siteDefinition.Hosts.Add(new HostDefinition { Name = siteDefinition.SiteUrl.Authority });
siteDefinition.Hosts.Add(new HostDefinition { Name = SiteDefinition.WildcardHostName });
_siteDefinitionRepository.Service.Save(siteDefinition);

Thank you for all your help!

// Anders

#88791
Jul 30, 2014 16:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.