2

I am currently creating a new page from code to use as my startpage for a sitedefinition which I also create from code..however.. even if I publish the newly created startpage I always end up with the following message in the CMS UI:

"This content is in English. It does not exist in svenska. Would you like to translate it now?"

How can I "Translate" the page from programmatically and then publish it as well? I haven't been able to find anything related to this here or while googling it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Inx51
  • 1,911
  • 3
  • 24
  • 44

1 Answers1

6

You need to use the CreateLanguageBranch available in the IContentRepository.

In my example below Swedish is the default language on the site

var parent = ContentReference.RootPage;

IContentRepository contentRepository = 
    EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
StartpagePage startpage = contentRepository.GetDefault<StartpagePage>(parent);

startpage.PageName = "Teststartsida";
startpage.Title = "Teststartsida";

// this will create a startpage in the default language, Swedish in my case, 
// use SaveAction.Publish and save the page into a new variable
var createdPage = contentRepository.Save(startpage,
    EPiServer.DataAccess.SaveAction.Publish, 
    AccessLevel.NoAccess);

// invoke CreateLanguageBranch with LanguageSelector
var startpageLanguageBranch = 
    contentRepository.CreateLanguageBranch<StartpagePage>(createdPage, 
        new LanguageSelector("en"));

startpageLanguageBranch.PageName = "Start page test";
startpageLanguageBranch.Title = "Start page test";

// this will create a languagebranch in the language stated with the LanguageSelector. 
// Use SaveAction.Save
contentRepository.Save(startpageLanguageBranch, 
    EPiServer.DataAccess.SaveAction.Save, 
    AccessLevel.NoAccess);
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157