Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Sitecore.Publishing.Pipelines.PublishItem;
using System;
using System.Linq;
using Sitecore.Data.Managers;
namespace Sitecore.Feature.Language
{
using Globalization;
public class AddItemVersionPipeline
{
public void AddItemVersions(object sender, EventArgs args)
{
ItemProcessedEventArgs itemProcessedEventArgs = args as ItemProcessedEventArgs;
PublishItemContext context = itemProcessedEventArgs != null ? itemProcessedEventArgs.Context : null;
if(context!=null)
{
// getting item from Master DB
var item = Sitecore.Configuration.Factory.GetDatabase("master").GetItem(context.ItemId);
// checking if the item has the specified template id
var itemTemplates = Sitecore.Configuration.Settings.GetSetting("Sagicor.PublishPipelineTemplateIds")?.Split('|');
if (itemTemplates.Any())
{
if (item != null)
{
if (itemTemplates.Contains(item.TemplateID.ToString()))
{
//getting all regions
Sitecore.Data.Fields.MultilistField regionMultilistField = item.Fields["Regions"];
if (regionMultilistField.TargetIDs.Any())
{
foreach (var region in regionMultilistField.TargetIDs)
{
var regionItem = Sitecore.Configuration.Factory.GetDatabase("master").GetItem(region);
var languageCode = regionItem.Fields["RegionCode"]?.Value;
//all the items are created in english language only
Language sourceLanguage = LanguageManager.GetLanguage("en", Sitecore.Configuration.Factory.GetDatabase("master"));
Language targetLanguage = LanguageManager.GetLanguage(languageCode, Sitecore.Configuration.Factory.GetDatabase("master"));
if(item!=null && sourceLanguage!=null && targetLanguage!=null && sourceLanguage!=targetLanguage)
{
Copy(item, sourceLanguage, targetLanguage);
}
}
}
}
}
}
}
}
void Copy(Sitecore.Data.Items.Item item, Language sourceLanguage,
Language targetLanguage)
{
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("web");
Sitecore.Data.Items.Item targetItem = masterDB.Items[item.ID, targetLanguage];
Sitecore.Data.Items.Item sourceItem = masterDB.Items[item.ID, sourceLanguage];
if (targetItem == null || sourceItem == null || sourceItem.Versions.Count == 0)
return;
using (new Sitecore.SecurityModel.SecurityDisabler())
{
try
{
if (targetItem.Versions.Count == 0)
{
targetItem = targetItem.Versions.AddVersion();
targetItem.Editing.BeginEdit();
sourceItem.Fields.ReadAll();
foreach (Sitecore.Data.Fields.Field field in sourceItem.Fields)
{
if (!field.Shared && !field.Name.StartsWith("__") && field.Name.Trim() != "")
{
targetItem.Fields[field.Name].SetValue(field.Value, true);
}
}
targetItem.Editing.EndEdit();
targetItem.Editing.AcceptChanges();
Sitecore.Diagnostics.Log.Info("Publish Success >> Item " + item.ID + " Content Copied From " + sourceLanguage + " To " + targetLanguage, this);
}
Sitecore.Diagnostics.Log.Info("Publish Success >> Item " + item.ID + " Content Already Present For " + targetLanguage, this);
}
catch (Exception ex)
{
targetItem.Editing.CancelEdit();
Sitecore.Diagnostics.Log.Error("Publish Failed >> Error Copying Content " + ex.InnerException, this);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.