Using Sitecores pipeline to pre-populate item in current language

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:

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!

This article is part of the GWB Archives. Original Author: Asif Maniar

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.