2016-08-03 50 views
2

我需要在项目发布之前更新Sitecore中的日期时间字段。当项目实际发布时,这将表现为“发布日期时间”。我已经在工作流中成功实现了这一功能,并且通过添加自定义操作可以在工作流中的项目中正常工作。发布之前的Sitecore更新字段

对于不在工作流程中并且应该由发布代理拾取的项目,我点击管道并在PerformAction处理器之前添加了一个处理器。该字段在主数据库中得到更新,但从未由发布代理发布到Web数据库。该字段更新之前的所有其他值的项目通过罚款。

我试图调试这个问题,并觉得它的发生,因为更新的项目不反映为publishqueue的一部分。有没有一种方法可以强制日期时间字段的更新也在同一过程中发布,而不必强制发布?

欢迎任何建议。

回答

2

你是正确的更新项目不是发布队列的一部分。您需要将代码放入发布:itemProcessing事件中。 您需要按照下面的步骤:

  1. 添加处理类为
<event name="publish:itemProcessing" help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore.Publishing.Pipelines.PublishItem)"/> 

您发布:itemProcessing事件会像

<event name="publish:itemProcessing" help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore.Publishing.Pipelines.PublishItem)"> 
     <handler type="YourNameSpace.SetPublishDate, YourAssembly" method="UpdatePublishingDate"/> 
    </event> 
  • 在发布时创建您自己的课程以处理项目:
  • public class SetPublishDate 
        { 
        /// <summary> 
        /// Gets from date. 
        /// </summary> 
        /// <value>From date.</value> 
        public DateTime FromDate { get; set; } 
    
        /// <summary> 
        /// Gets to date. 
        /// </summary> 
        /// <value>To date.</value> 
        public DateTime ToDate { get; set; } 
    
        public void UpdatePublishingDate(object sender, EventArgs args) 
        { 
         var arguments = args as Sitecore.Publishing.Pipelines.PublishItem.ItemProcessingEventArgs; 
         var db = Sitecore.Configuration.Factory.GetDatabase("master"); 
         Item item = db.GetItem(arguments.Context.ItemId); 
         if (item != null) 
         { 
          using (new Sitecore.Data.Events.EventDisabler()) 
          { 
           using (new EditContext(item)) 
           { 
            //PublishDateFieldName must be datetime field 
            item["PublishDateFieldName"] = DateTime.Now; 
           } 
          } 
         } 
        } 
    
    2

    根据你要使用这个日期,或许稍微不同的方法可能会更好。以前的答案是有效的,可能会工作得很好。但在发布时更新主数据库可能会让发布引擎认为主项目已更改并需要重新发布。 (EventDisabler等可以防止这种情况发生,也可以触发重新索引,等等......事情可能会变得非常棘手)

    或者,您可以改为在Web数据库中的项目上编写发布日期。

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
        <sitecore> 
        <pipelines> 
         <publishItem> 
         <processor type="Sitecore.Publishing.Pipelines.PublishItem.PerformAction, Sitecore.Kernel"> 
          <patch:attribute name="type">Your.Namespace.PerformAction, Your.Assembly</patch:attribute> 
         </processor> 
         </publishItem> 
        </pipelines> 
        </sitecore> 
    </configuration> 
    

    和实施与此类似:

    public class PerformAction : Sitecore.Publishing.Pipelines.PublishItem.PerformAction 
    { 
        public override void Process(PublishItemContext context) 
        { 
         base.Process(context); 
    
         if (context.Aborted || context.VersionToPublish == null || context.VersionToPublish.Source == null) 
          return; 
    
         var target = context.PublishOptions.TargetDatabase.GetItem(context.VersionToPublish.ID, context.VersionToPublish.Language); 
    
         if (target == null) 
          return; 
    
         using (new EditContext(target, false /*updateStatistics*/, true /*silent*/)) 
         { 
          DateField lastPublished = target.Fields["LastPublished"] 
          lastPublished.Value = Sitecore.DateUtil.IsoNo; 
         } 
        } 
    } 
    

    约翰西方有一个博客帖子这个位置:

    http://www.sitecore.net/learn/blogs/technical-blogs/john-west-sitecore-blog/posts/2011/08/intercept-item-publishing-with-the-sitecore-aspnet-cms.aspx

    具有存储在Web数据库中的发布日期,你可以从那里读取它,或者为主数据库创建一个计算索引字段,而不是来自Web数据库的日期。

    这可能是一个更强大的解决方案,但它又取决于您使用该字段的内容,以及您是否正在控制读取该值的代码。