2016-08-03 59 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數據庫的日期。

    這可能是一個更強大的解決方案,但它又取決於您使用該字段的內容,以及您是否正在控制讀取該值的代碼。