在 EpiServer 8 中,如何扩展 Scheduled Publish 事件?
In EpiServer 8, how can I extend the Scheduled Publish event?
我正在使用 EpiServer 8,需要在保存计划发布时执行自定义 API 调用。目前我能够通过初始化模块捕获即时发布事件,如下所示:
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEventInitializer : IInitializableModule
{
public void Initialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishingContent += EventsPublishingContent;
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishingContent -= EventsPublishingContent;
}
private void EventsPublishingContent(object sender, ContentEventArgs contentEventArgs)
{
// Tell our API that maintenance started.
}
}
以上 EventsPublishingContent 事件在编辑者立即发布内容时起作用。该方法的 Visual Studio 断点成功命中。但是当网站编辑选择 "Schedule for Publish."
时它不会执行
当编辑查看 "Schedule for Publish" 对话框并选择 "Schedule" 按钮时,我想捕获以下内容并将其发送到我们的 API:
- "Publish changes on" 值。
- 将要发布的页面。
执行此操作的正确方法是什么?谢谢
当使用 "Schedule for publish" 时,页面将由预定作业发布,我认为不会触发任何事件,至少看起来不像,更多信息在这里: http://world.episerver.com/blogs/Petra-Liljecrantz/Dates/2016/3/differences-between-scheduled-publish-and-normal-publish/
以下是我 运行 使用的解决方案的关键部分。最终,我在来自 IContentEvents 的大约十几个潜在事件中设置了断点,并隔离了 2 个对我的目的有用的事件。不幸的是,我不得不从 EpiServer 数据库中获取计划发布时间戳。代码中提供了一些TODO注释,这只是为了简化答案。
代码中的注释是基于我的观察。我不是 EpiServer 达人。
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
namespace MySite.Helpers
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEventInitializer : IInitializableModule
{
public void Initialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.CheckedInContent += checkedInContent;
events.PublishedContent += publishedContent;
}
public void Uninitialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.CheckedInContent -= checkedInContent;
events.PublishedContent -= publishedContent;
}
/// <summary>
/// Occurs when a version of a content item has been checked in.
/// </summary>
/// <remarks>
/// This is called after a scheduled publish gets saved. It's not called after an immediate publish.
/// Useful for executing custom events following a scheduled publish. When this event occurs,
/// you can fetch the publish timestamp from dbo.tblWorkContent.DelayPublishUntil.
/// Prior to this event the DelayPublishUntil value will be NULL.
/// </remarks>
public static void checkedInContent(object sender, ContentEventArgs contentEventArgs)
{
// Fetch timestamp from dbo.tblWorkContent.DelayPublishUntil.
ConnectInfo connectInfo = new ConnectInfo("MyEpiServerDatabase");
PlannedMaintenanceInfo plannedMaintenanceInfo = new PlannedMaintenanceInfo(ref connectInfo, contentEventArgs.Content.ContentLink.WorkID);
connectInfo = null;
// The PlannedMaintenanceInfo method above uses the following SQL query:
// string query = string.Format("SELECT URLSegment, DelayPublishUntil FROM tblWorkContent WHERE pkId = {0}", WorkID);
// TODO: Notify API about this planned maintenance window.
}
/// <summary>
/// Occurs when a content item or a version of a content item has been published.
/// </summary>
/// <remarks>
/// This is called after an immediate publish. It's not called after a scheduled publish gets saved.
/// Useful for executing custom events following an immediate publish.
/// </remarks>
public void publishedContent(object sender, ContentEventArgs contentEventArgs)
{
// TODO: Notify API that an immediate maintenance window has started.
}
}
}
为简洁起见,省略了数据检索代码和其他移动部分。如果您正在阅读本文,我假设您知道如何获取数据,并且无论如何都有自己的偏好或框架。我编写的实际 SQL 查询位于 checkedInContent 方法的注释中。所以你可以把它和 运行 一起放在你自己的数据库中。重要的是确保将 WorkID 传递给查询。该 ID 来自 contentEventArgs.Content.ContentLink.WorkID.
我希望 EpiServer 公开该时间戳值,该值最终会在表单提交期间保存到 tblWorkContent.DelayPublishUntil。这样就没有必要从数据库中获取值。反过来,这将使以更通用的方式重用它变得更容易,我们不需要提供数据库连接字符串来提取该时间戳。如果该值在某些 属性 中公开,而我只是想念它,请告诉我。
我希望这段代码能帮到别人。
我正在使用 EpiServer 8,需要在保存计划发布时执行自定义 API 调用。目前我能够通过初始化模块捕获即时发布事件,如下所示:
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEventInitializer : IInitializableModule
{
public void Initialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishingContent += EventsPublishingContent;
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishingContent -= EventsPublishingContent;
}
private void EventsPublishingContent(object sender, ContentEventArgs contentEventArgs)
{
// Tell our API that maintenance started.
}
}
以上 EventsPublishingContent 事件在编辑者立即发布内容时起作用。该方法的 Visual Studio 断点成功命中。但是当网站编辑选择 "Schedule for Publish."
时它不会执行当编辑查看 "Schedule for Publish" 对话框并选择 "Schedule" 按钮时,我想捕获以下内容并将其发送到我们的 API:
- "Publish changes on" 值。
- 将要发布的页面。
执行此操作的正确方法是什么?谢谢
当使用 "Schedule for publish" 时,页面将由预定作业发布,我认为不会触发任何事件,至少看起来不像,更多信息在这里: http://world.episerver.com/blogs/Petra-Liljecrantz/Dates/2016/3/differences-between-scheduled-publish-and-normal-publish/
以下是我 运行 使用的解决方案的关键部分。最终,我在来自 IContentEvents 的大约十几个潜在事件中设置了断点,并隔离了 2 个对我的目的有用的事件。不幸的是,我不得不从 EpiServer 数据库中获取计划发布时间戳。代码中提供了一些TODO注释,这只是为了简化答案。
代码中的注释是基于我的观察。我不是 EpiServer 达人。
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
namespace MySite.Helpers
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEventInitializer : IInitializableModule
{
public void Initialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.CheckedInContent += checkedInContent;
events.PublishedContent += publishedContent;
}
public void Uninitialize(InitializationEngine initializationEngine)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.CheckedInContent -= checkedInContent;
events.PublishedContent -= publishedContent;
}
/// <summary>
/// Occurs when a version of a content item has been checked in.
/// </summary>
/// <remarks>
/// This is called after a scheduled publish gets saved. It's not called after an immediate publish.
/// Useful for executing custom events following a scheduled publish. When this event occurs,
/// you can fetch the publish timestamp from dbo.tblWorkContent.DelayPublishUntil.
/// Prior to this event the DelayPublishUntil value will be NULL.
/// </remarks>
public static void checkedInContent(object sender, ContentEventArgs contentEventArgs)
{
// Fetch timestamp from dbo.tblWorkContent.DelayPublishUntil.
ConnectInfo connectInfo = new ConnectInfo("MyEpiServerDatabase");
PlannedMaintenanceInfo plannedMaintenanceInfo = new PlannedMaintenanceInfo(ref connectInfo, contentEventArgs.Content.ContentLink.WorkID);
connectInfo = null;
// The PlannedMaintenanceInfo method above uses the following SQL query:
// string query = string.Format("SELECT URLSegment, DelayPublishUntil FROM tblWorkContent WHERE pkId = {0}", WorkID);
// TODO: Notify API about this planned maintenance window.
}
/// <summary>
/// Occurs when a content item or a version of a content item has been published.
/// </summary>
/// <remarks>
/// This is called after an immediate publish. It's not called after a scheduled publish gets saved.
/// Useful for executing custom events following an immediate publish.
/// </remarks>
public void publishedContent(object sender, ContentEventArgs contentEventArgs)
{
// TODO: Notify API that an immediate maintenance window has started.
}
}
}
为简洁起见,省略了数据检索代码和其他移动部分。如果您正在阅读本文,我假设您知道如何获取数据,并且无论如何都有自己的偏好或框架。我编写的实际 SQL 查询位于 checkedInContent 方法的注释中。所以你可以把它和 运行 一起放在你自己的数据库中。重要的是确保将 WorkID 传递给查询。该 ID 来自 contentEventArgs.Content.ContentLink.WorkID.
我希望 EpiServer 公开该时间戳值,该值最终会在表单提交期间保存到 tblWorkContent.DelayPublishUntil。这样就没有必要从数据库中获取值。反过来,这将使以更通用的方式重用它变得更容易,我们不需要提供数据库连接字符串来提取该时间戳。如果该值在某些 属性 中公开,而我只是想念它,请告诉我。
我希望这段代码能帮到别人。