Sunday 15 June 2014

Remove specific language version from items in Sitecore

In this blog post; I am going to explain how to remove a specific language version from items in Sitecore programmatically. Currently; I have got into a situation where I have to remove a specific language version (fr-FR in my case) from Sitecore items of a multilingual website. Below function RemoveLanguageVersion(string rootItemPath, string languageName) takes the root item path and language name  as input parameter and remove given language version from root item and child items of root item.
public void RemoveLanguageVersion(string rootItemPath, string languageName)
            Language languageRemove = Sitecore.Globalization.Language.Parse(languageName);
            Item rootItem = Sitecore.Context.Database.GetItem(rootItemPath, languageRemove);

            if (rootItem != null)
                using (new Sitecore.SecurityModel.SecurityDisabler())
                    //Remove language version from root item               

                    //Remove language version recursively from child items of root item
                    foreach (Item child in rootItem.Axes.GetDescendants().Where(l => l.Language == languageRemove))
string rootItemPath = "/sitecore/content/Home/Audience";
string languageName = "fr-FR";

RemoveLanguageVersion(rootItemPath, languageName);
Above code will remove fr-FR language version from item /sitecore/content/Home/Audience and its child item recursively.

Comments and suggestions are most welcome. Happy coding!