Geeks With Blogs
Asif Maniar Software Engineer

Sitecore has a powerful event pipeline infrastructure that you can leverage to plugin commands into various item creation, change, move, publish etc events.

Recently I had to add functionality to Sitecore so that when an items version is created all fields from a target language are copied into the newly created version to ease editing.

To do this first we add an event handler in the Web.Config file for the versionAdded event.

 <event name="item:versionAdded">
        <handler type="MyNamespace.MyClass, MyAssembly" method="OnVersionAdded" />
      </event>

 

Then add the OnVersionAdded event to the class added to the type attribute above.

The Method is as shown:

namespace MyNameSpace{
    public class MyClass
    {
        public void OnVersionAdded(object sender, EventArgs args)
        {
           //get item
            var item = Event.ExtractParameter(args, 0) as Item;
            try
            {
                if (item != null && item.Version.Number == 1)
                { 
                        //copy fields from fallback item
                        //we were using Language Fallback as explained below. Get whatever sourceItem you want to use 
                        var fallbackItem = FallbackLanguageManager.GetFallbackItem(item, false);

                        item.Editing.BeginEdit();
                        fallbackItem.Fields.ReadAll();
                        //copy over all fields from source to target language
                        foreach (Sitecore.Data.Fields.Field field in fallbackItem.Fields)
                        {
                            if (!field.Shared && !field.Name.StartsWith("__") && field.Name.Trim() != "")
                            {
                                item.Fields[field.Name].SetValue(field.Value, true);
                            }
                        }
                        item.Editing.EndEdit();
                        item.Editing.AcceptChanges();
                    }
                }
            }
            catch (Exception exception)
            {
                Sitecore.Diagnostics.Log.Error("Application Error", exception, this);
                if (item != null)
                { 
                    item.Editing.CancelEdit(); 
                }
            }
    }
}
 
Note that above code uses a FallbackLanguageManager to get the fallback item. 
This is basically the current item in the fallback language configured for the current language.
This is based on the Fallback provider released by Alex Shyba at Sitecore:
http://sitecoreblog.alexshyba.com/2010/11/approaching-language-fallback-with.html
 
If you are not using the fallback simply change the line to get the Item from the desired source language like:
var fallbackItem = item.Database.GetItem(item.ID, Language.Parse("en"));
 
Code for the FallbackLanguageManager (borrowed from Alex’s fallback provider source)
 
public class FallbackLanguageManager
    {
        public static Item GetFallbackItem(Item item, bool forceFallback)
        {
            var fallbackLanguage = GetFallbackLanguage(item.Language, item.Database);
            var enforcedFallbackLanguage = Language.Parse("en");

            if (fallbackLanguage != null && !String.IsNullOrEmpty(fallbackLanguage.Name) && !fallbackLanguage.Equals(item.Language))
            {
                return item.Database.GetItem(item.ID, fallbackLanguage, Version.Latest);
            }

            if (forceFallback && !item.Language.Equals(enforcedFallbackLanguage))
            {
                return item.Database.GetItem(item.ID, enforcedFallbackLanguage, Version.Latest);
            }

            return null;
        }

        public static Language GetFallbackLanguage(Language language, Database database)
        {
            var sourceLangItem = GetLanguageDefinitionItem(language, database);
            return sourceLangItem != null ? GetFallbackLanguage(sourceLangItem) : null;
        }

        public static Item GetLanguageDefinitionItem(Language language, Database database)
        {
            var sourceLanguageItemId = LanguageManager.GetLanguageItemId(language, database);
            return ID.IsNullOrEmpty(sourceLanguageItemId) ? null : ItemManager.GetItem(sourceLanguageItemId, Language.Parse("en"), Version.Latest, database, SecurityCheck.Disable);
        }

        protected static Language GetFallbackLanguage(Item langItem)
        {
            var fallbackLangName = langItem[SitecoreFields.FallbackLanguage];
            Language fallbackLang;
            return Language.TryParse(fallbackLangName, out fallbackLang) ? fallbackLang : null;
        }
    }
 
Hope this helps!
Posted on Monday, December 12, 2011 5:41 PM ASP.NET , CMS , Sitecore | Back to top


Comments on this post: Using Sitecores pipeline to pre-populate item in current language

comments powered by Disqus

Copyright © Asif Maniar | Powered by: GeeksWithBlogs.net