2016-06-14 40 views
2

我正在使用EpiServer 8,并且需要在保存计划发布时执行自定义API调用。目前,我能够通过初始化模块捕捉即时发布事件,就像这样:当一个编辑器立即发布内容在EpiServer 8中,如何扩展Scheduled Publish事件?

以上EventsPublishingContent
[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. 
    } 
} 

事件的作品。该方法上的Visual Studio断点成功命中。但是,当网站编辑选择“发布时间表”时,它不会执行。

当一个编辑器查看“时间表发布”对话框中,选择“日程安排”按钮,我想捕捉以下,并将其发送给我们的API:

  • “发布变化对“价值。
  • 将要发布的页面。

这样做的正确方法是什么?谢谢。

+1

计划发布之间的时间窗口有多长?您确定在编辑器操作和实际预定作业执行之间没有应用程序池回收吗?查看该作业的代码 - 它将内容存储库上的Publish()动作称为应该触发发布/发布的事件。 –

+0

谢谢Valdis。目前这个项目还在开发中,我是编辑进行编程和测试。基本上我想拦截“Schedule for Publish”事件,这样我就可以发布发布时间戳和发布到单独API的页面。 –

+1

听起来像你可能会连接* ContentSaved *事件并查看内容的* StartPublish *属性? –

回答

1

以下是我使用的解决方案的关键部分。最终,我在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。这样就不需要从数据库中获取值。这反过来会更容易以更通用的方式重新使用它,我们不需要提供数据库连接字符串来提取该时间戳。如果这个价值暴露在一些财产,我只是想念它,请让我知道。

我希望这段代码可以帮助别人。

1

当使用“发布时间表”时,该页面将按计划发布,我不认为会触发任何事件,至少看起来并不像这样,更多信息在这里: http://world.episerver.com/blogs/Petra-Liljecrantz/Dates/2016/3/differences-between-scheduled-publish-and-normal-publish/

+0

感谢您的链接。也许我需要创建一个任务来查看tblWorkContent中的DelayPublishUntil字段。当计划的发布被保存时,这似乎是价值所在。如果我可以在“时间表”按钮上捕获该点击事件,那就太好了。 –