2012-10-29 47 views
4

[編輯]我實際上已被允許使用文檔名稱,這使得它更容易,但我仍然認爲這將是有趣的,找出是否有可能。複製內容保存爲一個多語言的umbraco網站

我必須設置一個觸發器將內容複製到內容樹上的不同分支,因爲該網站將使用多種語言。我被告知,我無法通過名稱訪問文檔(因爲它們可能會發生變化),也不應該使用節點標識符(不是我會知道如何在一段時間之後很難遵循該結構)。

如何遍歷樹來將新文檔插入到其他語言的相關子分支中?有沒有辦法?

回答

3

您可以使用Document.AfterPublish事件在發佈後捕獲特定的文檔對象。我將使用此事件處理程序來檢查節點類型別名是否需要複製,然後可以調用Document.MakeNew並傳遞新位置的節點標識。 這意味着您不必使用特定的節點標識或文檔名稱來捕獲事件。

例子:

using umbraco.cms.businesslogic.web; 
using umbraco.cms.businesslogic; 
using umbraco.BusinessLogic; 

namespace MyWebsite { 
    public class MyApp : ApplicationBase { 
     public MyApp() 
      : base() { 
      Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish); 
     } 

     void Document_AfterPublish(Document sender, PublishEventArgs e) { 
      if (sender.ContentType.Alias == "DoctypeAliasOfDocumentYouWantToCopy") { 
       int parentId = 0; // Change to the ID of where you want to create this document as a child. 
       Document d = Document.MakeNew("Name of new document", DocumentType.GetByAlias(sender.ContentType.Alias), User.GetUser(1), parentId) 
       foreach (var prop in sender.GenericProperties) { 
        d.getProperty(prop.PropertyType.Alias).Value = sender.getProperty(prop.PropertyType.Alias).Value; 
       } 
       d.Save(); 
       d.Publish(User.GetUser(1)); 
      } 
     } 
    } 
} 
+0

非常感謝你。我實際上已經使用Document.New對它進行了排序,因爲它沒有必要立即發佈。說,你的簡潔得多。 –

+0

而且實際上會幫助另一部分,所以雙感謝隊友。 –