Sitefinity ASP.NET CMS - Content Management System

KB Article

Home >  Support >  Knowledge Base >  KB Article
Automatically create other language versions of the page using Sitefinity API - ID#1046
Rating: Not rated
Last Modified: 7/10/2008
Related categories: Localization;

Article information

Article relates to

 Sitefinity 3.2

Created by

 Vladimir Vasilev

Last modified by

 Rebecca


Introduction

This example demonstrates how to implement a functionality for automatic creation of other language versions of the page when the default language version of the page is published. In order to handle the "PagePublished" event, we will use the Sitefinity services model (implementing IService type and declaring it in the telerik/framework/services section in the web.config).

Example

1. Create GlobalService.cs file in the ~/App_Code folder of your Web Site:

using System; 
using Telerik.Framework; 
using Telerik.Cms; 
using System.Globalization; 
using Telerik.Localization; 
using System.Threading; 
 
public class GlobalService : IService 
    public GlobalService() 
    { 
    } 
    #region IService Members 
 
    public void Initialize() 
    { 
        CmsManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(CmsManager_Executed); 
    } 
 
    void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e) 
    { 
        if (e.CommandName == "Publish"
        { 
            ICmsPage cmsPage = sender as ICmsPage; 
            if (cmsPage != null && LocalizationManager.IsDefaultCulture) 
            { 
                // saves the original current culture 
                CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture; 
                 
                CmsManager manager = new CmsManager(); 
 
                int sourceLangID = (cmsPage.LangID == CultureInfo.InvariantCulture.LCID) ? LocalizationManager.DefaultCulture.LCID : cmsPage.LangID; 
                foreach (CultureInfo culture in LocalizationManager.Cultures.Values) 
                { 
                    if (culture.LCID == sourceLangID) 
                        continue
 
                    if (!cmsPage.LanguageVersions.ContainsKey(culture.LCID)) 
                    { 
                        // changes the culture of the current thread 
                        Thread.CurrentThread.CurrentUICulture = culture; 
 
                        // Gets the Cms page for the new culture 
                        ICmsPage tempPage = (ICmsPage)manager.GetPage(cmsPage.ID, true); 
 
                        // Checks out the Cms page creating new staged version 
                        IStagedPage staged = tempPage.Staged.CheckOut(); 
 
                        // Copies the content from another language version and saves in into database. 
                        staged.CopyFromLanguage(sourceLangID); 
 
                        // Gets the page from the database, and checkin (Publish) 
                        tempPage = (ICmsPage)manager.GetPage(cmsPage.ID, true); 
                        tempPage.Staged.CheckIn(); 
                    } 
                } 
 
                // restores the original culture 
                Thread.CurrentThread.CurrentUICulture = currentCulture; 
            } 
        } 
    } 
    #endregion 
 

2. Declare it in the web.config:

<telerik>  
    <framework>  
        <services>  
            <add type="GlobalService, app_code"/> 





Article Comments

There are no comments yet.
Please Sign In to rate this article or to add it to your favorites.