More in this section

Forums / Developing with Sitefinity / How to create multilingual pages programmatically?

How to create multilingual pages programmatically?

4 posts, 0 answered
  1. Dave
    Dave avatar
    16 posts
    Registered:
    10 Feb 2010
    27 May 2011
    Link to this post
    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

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    30 May 2011
    Link to this post
    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
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Dave
    Dave avatar
    16 posts
    Registered:
    10 Feb 2010
    30 May 2011
    Link to this post
    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]);
                   }
               }



    Hmm well the Exception makes sense, but what is the right way?

    Thanks in advance :)

    Dave
  4. Nicolas
    Nicolas avatar
    156 posts
    Registered:
    19 Jan 2011
    30 May 2011
    Link to this post
    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);
     
    Don't create a PageData manually :

    PageDraft pDraft = mMgr.EditPage(node.Page.Id, false);
    PageData pData = pDraft.ParentPage;

    For your error :
    node.UrlName["en"] = new Lstring(pageNameEn);
    node.UrlName["fr"] = new Lstring(pageNameFr);

    Regards,
    Nicolas
4 posts, 0 answered