2012-10-30 20 views
5

我想要獲取RepositoryLocalObject(例如Component)繼承自的父項的列表。因此,如果我有一個包含組件tcm:1-80的pub ID 1和一個子pub ID 2,則該組件將在子pub中共享爲tcm:2-80。所以我想要得到tcm:2-80的父母,或者樹上的任何東西都在向上移動。Tridion RepositoryLocalObject.GetBluePrintChain方法(BluePrintChainFilter)在共享項目上產生異常

我已經嘗試GetBluePrintChain()方法在組件的本地副本,它工作正常。但是,在共享組件上,它返回一個InvalidActionException:「這個項目是共享的」。文檔中提到這個異常是在共享項目上引發的。但這是如何有意義的?顯然,如果任何有藍圖鏈條的項目將被共享(或成爲本地副本)。所以對我來說,讓這個方法在有藍圖鏈的東西上拋出異常是沒有意義的。這似乎是矛盾的。

我的問題與Getting root publication of component有些相關,但它有所不同。我需要理解爲什麼這個異常拋出共享項目。有人可以解釋,也許可以分享一個用例來支持它嗎?

回答

4

據我所知GetBluePrintChain方法是爲了讓你在站在頂端時看不起BluePrint。因此,對於您的情況,您應該在自己的發佈上下文中獲取該項目,然後調用GetBluePrintChain

Item item = package.GetByName("Component"); 
Component component = new Component(item.GetAsXmlDocument().DocumentElement, 
            engine.GetSession()); 
TcmUri id = TemplateUtilities.CreateTcmUriForPublication(
     component.OwningRepository.Id.ItemId, component.Id); 

var blueprintchain = ((Component)engine.GetObject(id)).GetBluePrintChain(); 

package.PushItem(package.CreateStringItem(ContentType.Text, 
              blueprintchain.ToString())); 
package.PushItem(package.CreateStringItem(ContentType.Text, 
          ""+System.Linq.Enumerable.Count(blueprintchain))); 
foreach (var item in blueprintchain) 
{ 
    package.PushItem(package.CreateTridionItem(ContentType.Component, item)); 
} 

我只是跑上述C#片段作爲TBB在兩種方案:

  1. 在子出版物上的共享組件
  2. 在子出版物的局部組件

在情況1下,blueprintchain將包含單個項目:共享組件。在案例2中,blueprintchain將包含兩個項目:共享組件和本地化組件。

+1

啊,但是如果你指定一個過濾器,你可以設置方向向上或向下。所以我希望能夠採取共享項目,並鏈上樹: BluePrintChainFilter bpfilter = new BluePrintChainFilter(BluePrintChainDirection.Up,engine.GetSession()); –

+1

可以將項目本地化到當前上下文存儲庫中,在這種情況下,您可以向上或向下查看 - 並指定過濾器中的方向。無論哪種方式,似乎只能訪問其OwningRepository上下文中的項目上的BluePrintChain。 –

3

總結上面的答案,這裏有一個實際工作中,各地的「項目共享」的問題:

調用GetBluePrintChain()對於出現這種情況是共享將失敗的任意的項目:

return 
    item.GetBluePrintChain(
    new BluePrintChainFilter(
     BluePrintChainDirection.Up, 
     engine.GetSession() 
    ) 
).LastOrDefault(); 

的解決方案是先找到一個最頂端的本地化項目的父按照弗蘭克的食譜:

return 
    ((RepositoryLocalObject)engine 
    .GetObject(
     TemplateUtilities.CreateTcmUriForPublication(
     item.OwningRepository.Id.ItemId, 
     item.Id 
    ) 
    ) 
).GetBluePrintChain(
    new BluePrintChainFilter(
     BluePrintChainDirection.Up, 
     engine.GetSession() 
    ) 
).LastOrDefault();