Skip to content

Instantly share code, notes, and snippets.

@merkle-sitecore-gists
Last active June 27, 2019 13:15
Show Gist options
  • Save merkle-sitecore-gists/4cd2a61bec813ef93df94a96708ce05a to your computer and use it in GitHub Desktop.
Save merkle-sitecore-gists/4cd2a61bec813ef93df94a96708ce05a to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Text.RegularExpressions;
using Sitecore.Data.Items;
using Sitecore.Rules;
using Sitecore.Rules.Actions;
namespace TAC.Internet.Web.CMS.Client.Rules.ItemSaved
{
   public class CopyFieldToField<T> : RuleAction<T> where T : RuleContext
   {
       public string FieldSource
       {
           get;
           set;
       }
       public string FieldTarget
       {
           get;
           set;
       }
       public override void Apply(T ruleContext)
       {
           try
           {
               using (new EditContext(ruleContext.Item, true, false))
               {
                   var item = ruleContext.Item;
                   item.Editing.BeginEdit();
                   var fieldValue = Regex.Replace(item.Fields[FieldSource].Value, "<.*?>", string.Empty);
                   item.Fields[FieldTarget].Value = WebUtility.HtmlDecode(fieldValue);
                   item.Editing.EndEdit();
               }
           }
           catch (Exception e)
           {
               log.Warn(e, string.Format("Rule 'Copy Field To Field' threw an exception on item ID {0}", ruleContext.Item.ID));
           }
       }
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment