Scott
Scott

Reputation: 1021

Run code when Publishing Restriction is saved in Sitecore

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

How would I go about doing that?

Upvotes: 0

Views: 216

Answers (1)

Lukas Kozak
Lukas Kozak

Reputation: 238

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);
        }
    }
}

Upvotes: 2

Related Questions