Add CultureSpecific to an exisiting block area – keep the data

Recently we built a globalized site in EPiServer 7. When the page was filled with content, the client wished that the block area should be the same for all languages.

Once the page was launched the customer realized that they needed to have different blocks in different languages, which meant that we needed to make the block area CultureSpecific. When making such a change, it means that the master language will keep the value of the block area, while all other languages will have a null value.

To get around this, I wrote a script that loops through all the pages from the master language, then retrieves the other language versions of the same page, and if the block area is null, we set the block area value to the same as the master language.

If you need to do this, make sure that you try this on a development / test environment first.


[ScheduledPlugIn(DisplayName="Populate block area")]
public class PopulateBlockArea
{
private static int changedPages = 0;
private const string masterLang = "en";
public static string Execute()
{
changedPages = 0;
var ls = new LanguageSelector(masterLang);

var start = DataFactory.Instance.GetPage(PageReference.StartPage, ls) as BasePage;

Populate(start);

foreach (var pageRef in DataFactory.Instance.GetDescendents(start.ContentLink.ToPageReference()))
{
var page = DataFactory.Instance.GetPage(pageRef, ls) as BasePage;
if (page != null)
{
Populate(page);
}
}

return string.Format("Changed values of {0} pages.", changedPages);
}

private static void Populate(BasePage page)
{
foreach (var lang in page.ExistingLanguages)
{
if (page.MainContentArea != null && lang.TwoLetterISOLanguageName != page.Language.TwoLetterISOLanguageName)
{
var langPage = DataFactory.Instance.GetPage(page.ContentLink.ToPageReference(), new LanguageSelector(lang.TwoLetterISOLanguageName));
if (langPage != null && langPage is BasePage)
{
var writePage = langPage.CreateWritableClone() as BasePage;
if (writePage.MainContentArea != null)
{
continue;
}
writePage.MainContentArea = page.MainContentArea;

DataFactory.Instance.Save(writePage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
changedPages++;
}
}
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA image
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>