Hi Friends,
Today we got some
requirement for the multilingual site, the requirement was to create a new
language and set the version for all the items in the content tree through the code.
Here is the sample code to achieve this task.
var homeItemA = Sitecore.Context.Database.GetItem(Landings.HomeItem);
var languageA = Sitecore.Globalization.Language.Parse("en-SG");//new language version
var master = Sitecore.Configuration.Factory.GetDatabase("master");
var allItems = master.GetItem(homeItemA.Paths.FullPath).Axes.GetDescendants();
foreach (var blog in allItems)
{
Item ca = master.GetItem(blog.Paths.FullPath, languageA);
using (new Sitecore.SecurityModel.SecurityDisabler())
{
try
{
if (0 == ca.Versions.Count)
{
ca.Versions.AddVersion();
}
}
catch (Exception ex)
{
ca.Editing.CancelEdit();
}
}
}