Scott
Scott

Reputation: 1021

Sitecore automatically sets publishing restrictions on item when edited

Our instance of Sitecore is auto setting a publishing restriction on the item with the current date/time when a user selects Edit. This only appears to happen when RequireLockBeforeEditing=true. Is this expected behavior? Why does this happen and is there a way to turn it off?

Upvotes: 0

Views: 759

Answers (1)

Derek Hunziker
Derek Hunziker

Reputation: 13141

<setting name="RequireLockBeforeEditing" value="true" /> is the default setting and will result in a new item Version being placed into Draft when the author selects Edit. In this senario, it's normal for Sitecore to assign a Publishable From date/time to the new version. This is not so much a restriction as it is a date/time stamp of when that version was created.

The date is needed as it is used to determine which version should be live. If there is no "from" date set, the version will not be publishable.

As for your custom code, if you post it here, I may be able to help you avoid it conflicting with Sitecore's default behavior. There may be some simple checks that you can do such as ignoring the Edit if the publishable from date matches the last updated date.

enter image description here

EDIT:

Instead of modifying Sitecore's default behavior (which will probably bite you later on) consider checking to see if the item being edited is brand new. When a new version is added, the item's created date and Valid from fields will match.

public void OnItemSaving(object sender, EventArgs args)
{
    try
    {
        Item item = Event.ExtractParameter(args, 0) as Item;
        ItemChanges itemChanges = Event.ExtractParameter(args, 1) as ItemChanges;

        // Ensure that a change was made to the valid to/from fields
        if (item != null &&
            itemChanges != null &&
            itemChanges.FieldChanges.ContainsAnyOf(FieldIDs.ValidFrom, FieldIDs.ValidTo))
        {
            // Ensure that the item is not brand new (such as when an author locks or clicks Edit)
            if (item.Publishing.ValidTo != DateTime.MaxValue ||
                item.Publishing.ValidFrom.ToString("MMddyyyyHHmmss") !=
                item.Statistics.Created.ToString("MMddyyyyHHmmss"))
            {
                // Do work here...
            }
        }
    }
    catch (Exception ex)
    {
        Log.Error("Error in item:saved event", ex, this);
    }
}

Upvotes: 5

Related Questions