2015-07-20 66 views
0

我正在開發Umbraco 7 MVC應用程序,我的要求是在Umbraco內部添加項目商品名稱應該是唯一的。對於用下面的代碼,但我得到的錯誤「哎呀呀:本文件發佈,但不是在緩存(內部錯誤)」Umbraco unpublish事件不適用於當前節點

 protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, 
      ApplicationContext applicationContext) 
     { 
      ContentService.Publishing += ContentService_Publishing; 
     } 


     private void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e) 
     { 
      try 
      { 
       if(newsItemExists) 
       { 
        e.Cancel = true; 
       } 
      } 
      catch (Exception ex) 
      { 
       e.Cancel = true; 
       Logger.Error(ex.ToString()); 
      } 
     } 

然後我嘗試添加代碼,以取消發佈,但它不工作即節點正在發佈。下面是我的代碼

private void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e) 
     { 
      try 
      { 
       int itemId=1234; //CurrentPublishedNodeId 
       if(newsItemExists) 
       { 
        IContent content = ContentService.GetById(itemId); 
        ContentService.UnPublish(content); 
        library.UpdateDocumentCache(item.Id); 
       } 
      } 
      catch (Exception ex) 
      { 
       e.Cancel = true; 
       Logger.Error(ex.ToString()); 
      } 
     } 

但與上面的代碼,如果你給CurrentPublishedNodeId = 2345 //正確someOthernodeId其未公佈。

你能幫我解決這個問題嗎?

回答

2

您不必這樣做,如果該項目已經存在(因此它是唯一的),Umbraco會自動附加(1)到名稱。

如果不希望這種行爲,您可以通過以下方式查詢:

protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 
{ 
    ContentService.Publishing += ContentService_Publishing; 
} 

private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e) 
{ 
    var contentService = UmbracoContext.Current.Application.Services.ContentService; 

    // It's posible to batch publish items, so go through all items 
    // even though there might only be one in the list of PublishedEntities 
    foreach (var item in e.PublishedEntities) 
    { 
     var currentPage = contentService.GetById(item.Id); 

     // Go to the current page's parent and loop through all of it's children 
     // That way you can determine if any page that is on the same level as the 
     // page you're trying to publish has the same name 
     foreach (var contentItem in currentPage.Parent().Children()) 
     { 
      if (string.Equals(contentItem.Name.Trim(), currentPage.Name.Trim(), StringComparison.InvariantCultureIgnoreCase)) 
       e.Cancel = true; 
     } 
    } 
} 

我認爲你的問題可能是你沒有通過所有PublishedEntities循環,但使用一些其他的方式來確定當前頁面ID。

注意:請不要使用library.UpdateDocumentCache這個,絕對沒有必要,ContentService.UnPublish會照顧緩存狀態。

+0

謝謝你的回覆。我的客戶想要顯示錯誤消息,如「該項目存在」。我正在使用PublishedEntities獲取當前的頁面ID,我將刪除這個「library.UpdateDocumentCache」。在行IContent content = ContentService.GetById(itemId);如果我通過currentpublilshedNodeId它不工作,但如果我給其他節點Id然後它的工作。 –

+0

你上面試過我的代碼嗎?不要使用currentPublishedNodeId。你也可以在這裏閱讀自定義消息:http://issues.umbraco.org/issue/U4-5927 - 現在有點可能,但有點不好意思,它在7.3.0本來就是可能的。 – sebastiaan

相關問題