Creating items and edit them afterwards in all languages

Today my dear colleague Max had the problem he wasn’t able to edit his newly created item in another language then the default one. So I helped poor old Max a little out by taking the creation of the item from him and supplying the item in all different languages to his delegate:

    1         public delegate void ItemByLanguageEditor(Item item);

    2 

    3         public void CreateItemFromMaster(string itemName, ID masterId, Item destination, ItemByLanguageEditor ible)

    4         {

    5             Assert.IsNotNullOrEmpty(itemName, “The parameter with the name ‘itemName’ cannot be null or empty”);

    6             Assert.IsNotNull(masterId, “The parameter with the name ‘masterId’ cannot be null”);

    7             Assert.IsNotNull(masterId, “The parameter with the name ‘destination’ cannot be null”);

    8 

    9             Item newCreatedItem = Sitecore.Data.Managers.ItemManager.AddFromMaster(itemName, masterId, destination);

   10             if(newCreatedItem != null)

   11             {

   12                 foreach (Sitecore.Globalization.Language l in newCreatedItem.Languages)

   13                 {

   14 

   15                     Item langVersionOfItem = Sitecore.Context.ContentDatabase.GetItem(newCreatedItem.ID, l);

   16                     ible.Invoke(langVersionOfItem);

   17 

   18                 }

   19             }

   20         }

When you use this method make sure you are/have:

  1. in the right security context
  2. selectected the right active site(Sitecore.Context.SetActiveSite….)
  3. the destination item(his upcoming parent) is in the right database

A small improvement to think about, for my friends in Denmark, would be a method with the following stub ;):

Item.GetByLanguage(Sitecore.Globalisation.Language lang):Item

2 thoughts on “Creating items and edit them afterwards in all languages”

Comments are closed.