How to create multilingual pages programmatically?
Hi Sitefinity Guys
I have to generate some pages (like 5000) out of a sql database. To create a simple page is easy.
But in my case i have 5 Languages in the system and i want to generate the language versions of each page too.
But how?
I tried some approaches but they all failed.
Can you give me a hint? or better an example code?
Regards :)
Dave
Hello Dave,
It depends on you want to have synced or split pages. For synced pages you should only change the CurrentUICulture and keep working the with the PageManager. This will create a page on the current language.
Here is the programmatic way
var pageNode = pageManager.CreatePage(PageLocation.Frontend, Guid.Empty, NodeType.Standard);
var page = pageNode.Page;
page.LocalizationStrategy = Localization.LocalizationStrategy.Split;
page.UiCulture = lang.Name;
page.Culture = lang.Name;
page.IsAutoCreated = true;
pageNode.UrlName.SetString(lang, sourceNode.UrlName[CultureInfo.InvariantCulture]);
Regards,
Ivan Dimitrov
the Telerik team
Thanks for your reply.
I built your code in, but i get an Exception:
You cannot save your changes, becase a page with such URL already exists. Change the page URL and try again
// Create a node in the SiteMap for that page.
PageNode node = mMgr.CreatePageNode();
mMgr.ChangeParent(node, pParentnode);
pParentnode.Nodes.Add(node);
// set the node properties
node.Title = pTitle;
node.ShowInNavigation =
true
;
node.UrlName = pTitle;
PageData pageData = mMgr.CreatePageData();
var alltemplates = mMgr.GetTemplates();
foreach
(var template
in
alltemplates)
if
(template.Title.Equals(pTemplate))
pageData.Template = template;
pageData.HtmlTitle = pTitle;
pageData.Title = pTitle;
pageData.LocalizationStrategy = LocalizationStrategy.Synced;
pageData.Culture =
"en"
;
pageData.UiCulture =
"en"
;
// Set the page status as published. The default status is draft
pageData.Status = ContentLifecycleStatus.Live;
// associate the node with the PageData object
node.Page = pageData;
foreach
(var lang
in
currentSettings.DefinedFrontendLanguages)
if
(!lang.TwoLetterISOLanguageName.ToLower().Equals(
"en"
))
var pageNode = mMgr.CreatePage(PageLocation.Frontend, Guid.Empty, NodeType.Standard);
var page = pageNode.Page;
page.LocalizationStrategy = LocalizationStrategy.Split;
page.UiCulture = lang.Name;
page.Culture = lang.Name;
page.IsAutoCreated =
true
;
pageNode.UrlName.SetString(lang, node.UrlName[CultureInfo.InvariantCulture]);
Hi,
I try this :
// Create a node in the SiteMap for that page.
PageNode node = mMgr.CreatePageNode();
mMgr.InitializePageLocalizationStrategy(node , Telerik.Sitefinity.Localization.LocalizationStrategy.Synced,
true
);
PageDraft pDraft = mMgr.EditPage(node.Page.Id,
false
);
PageData pData = pDraft.ParentPage;
node.UrlName[
"en"
] =
new
Lstring(pageNameEn);
node.UrlName[
"fr"
] =
new
Lstring(pageNameFr);