SaaS CMS has officially launched! Learn more now.

How can I capture the "schedule for publish" time within a ContentEventInitializer?

Vote:
 

When a site administrator edits a page and uses the "Schedule for Publish" option, I need to capture the "Publish changes on" date/time value.  Where I'm capturing this is within the context of a Content Event Initializer.  Here is part of the helper class I'm using for this:

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEventInitializer : IInitializableModule
{
    public void Initialize(InitializationEngine initializationEngine)
    {
        SetIsMonitorNotificationEnabled();
        if (IsMonitorNotificationEnabled)
        {
            var events = ServiceLocator.Current.GetInstance();
            events.CheckedInContent += ExtendEvent.checkedInContent;
        }
    }

And this is my checkedInContent event that executes when the "Schedule" button is selected.

private static class ExtendEvent
{
    public static void checkedInContent(object sender, ContentEventArgs contentEventArgs)
    {
        // Get the scheduled date and time.
    }
}

My initial solution was to write a method that retrieved the value from the database via the WorkID found in contentEventArgs, via:

contentEventArgs.Content.ContentLink.WorkID

And later I appended that WorkID to a SQL query, like so, to grab that DelayPublishUntil value from the database.

string query = string.Format("SELECT DelayPublishUntil FROM tblWorkContent WHERE pkId = {0}", WorkID);
using (SqlConnection connection = new SqlConnection(connectString))
{
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow))
        {
            IsDataRetrieved = reader.HasRows;
            while (reader.Read())
            {
                DelayPublishUntil = ReaderUtility.GetDateTimeNullable(reader, "DelayPublishUntil");
            }
        }
    }
}

For a variety of reasons, I really don't like using this approach to retrieve the date-time.  Primarily I'm concerned about foreword compatibility.  So how can I get that scheduled publish date and time in a more robust manner. Thanks!

#162164
Oct 11, 2016 20:57
Vote:
 

Hi,

You can load a ContentVersion through the API and there you have DelayPublishUntil. Remember to pass the PageReference/ContentReference with the WorkID as well (it should already be included in contentEventArgs.ContentLink.

#162169
Oct 11, 2016 23:27
Vote:
 

Thank you Johan. ContentVersion was the key.  This was my solution.  If there is a more efficient way to do this, let me know.

private DateTime? GetDelayPublishUntil(ContentEventArgs contentEventArgs)
{
    DateTime? DelayPublishUntil = null;
    var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentVersionRepository>();
    if (repository != null)
    {
        ContentVersion contentVersion = repository.Load(contentEventArgs.ContentLink);
        if (contentVersion != null)
        {
            DelayPublishUntil = contentVersion.DelayPublishUntil;
            contentVersion = null;
        }
        repository = null;
    }
    return DelayPublishUntil;
}
#162208
Oct 12, 2016 14:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.