Run code when Publishing Restriction is saved in Sitecore

后端 未结 1 1127
醉酒成梦
醉酒成梦 2021-01-29 00:53

I need to run code when an author saves a publishing restriction for an item.

How would I go about doing that?

相关标签:
1条回答
  • 2021-01-29 01:38

    The time restrictions are stored in the "__Valid to" and "__Valid from" fields. Attach a new pipe like this:

     <event name="item:saved">
            <handler type="Test.ValidTest, Test" method="OnItemSaved" />
     </event>
    

    And then test if those fields changed and do your thing:

    public class ValidTest
    {
        private static readonly ID __Validfrom = new ID("{C8F93AFE-BFD4-4E8F-9C61-152559854661}");
        private static readonly ID __Validto = new ID("{4C346442-E859-4EFD-89B2-44AEDF467D21}");
    
        public void OnItemSaved(object sender, EventArgs args)
        {
            Item obj = Event.ExtractParameter(args, 0) as Item;
            if (obj == null)
                return;
            //if (!(obj.TemplateID == YourTemplateId)) //restrict this to a limited set of templates if possible
            //    return;
            try
            {
                ItemChanges itemChanges = Event.ExtractParameter(args, 1) as ItemChanges;
                if (itemChanges != null &&
                    (itemChanges.FieldChanges.Contains(__Validfrom) || itemChanges.FieldChanges.Contains(__Validto)))
                {
                    //YOUR THING here      
                    Log.Info("Changed!", (object)this);
                }
            }
            catch (Exception ex)
            {
                Log.Error("failed", ex, (object)this);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题