Wednesday, February 11, 2015

Sitecore :- How to create new language version for all items




Hi Friends,

Today we got  some requirement for the multilingual site, the requirement was to create new language and set the version for all the items in the content tree.

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();

                    }
                }
            }