我正在使用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断点将被成功击中。但是,当站点编辑器选择“计划发布”时,它不会被执行。
当编辑器正在查看“发布计划”对话框并选择"Schedule“按钮时,我想捕获以下内容并将其发送到我们的API:
做这件事的正确方法是什么?谢谢。
发布于 2016-06-17 16:26:17
下面是我运行的解决方案中的关键部分。最后,我在来自IContentEvents的十几个潜在事件中插入了断点,并分离出了两个对我的目的有用的事件。不幸的是,我不得不从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。这样就不需要从数据库中获取值了。这样就可以更容易地以一种更通用的方式重用它,在这里,我们不需要提供数据库连接字符串来提取时间戳。如果这个价值暴露在某个财产中,而我只是错过了它,请告诉我。
我希望这段代码能帮上忙。
发布于 2016-06-14 20:05:22
当使用“发布计划”时,页面将由计划作业发布,我不认为任何事件都会由此触发,至少看起来不像,这里有更多信息:http://world.episerver.com/blogs/Petra-Liljecrantz/Dates/2016/3/differences-between-scheduled-publish-and-normal-publish/
https://stackoverflow.com/questions/37821077
复制相似问题