1

私はVBScriptで以下のコードを持っています

' Retrieve the keyword category for page section names
Set SectionCat = TDSE.GetObject(WebdavToUri(getPublicationWebDav(WEBDAV_SECTION_CAT)), 1)
' Retrieve the localized section keyword
Set SectionKeyword = SectionCat.GetKeywordByTitle(meta)

' Open the English translated section keyword
Set SectionKeyword = TDSE.GetObject(SectionKeyword.Id, 1, WEBDAV_UKEN_PUB)

SectionName = SectionKeyword.Title

WEBDAV_UKEN_PUBがWebDavPathである場合、VBScript の GetObject メソッドには、1) Item.ID、2) TDSDefines.OpenModeEditWithFallback、および 3) オブジェクトを作成する場所からの WebDavPath の 3 つのパラメーターを渡すオプションがあります。

今、私は 2009 .Net テンプレートで同じロジックを書きたいと思っています。以下はサンプル コードです。書き込もうとしていますが、VBScript オブジェクトを取り除くことができません。

Category cat = engine.GetSession().GetObject(WebdavToUri(getPublicationWebDav(Constants.WEBDAV_SECTION_CAT,package,engine), engine)) as Category;
if (cat != null)
{
//_log.Info("Category" + cat.Title);
Keyword keyword = cat.GetKeywordByTitle(meta);
//_log.Info("keyword 1" + keyword.Title);

keyword = engine.GetObject(Constants.WEBDAV_UKEN_PUB) as Keyword;

//_log.Info("keyword 2 " + keyword.Title);
if (keyword != null)
{
sectionName = keyword.Title;
}
keyword = null;

Category オブジェクトを作成することはできますが、Keyword オブジェクトを作成しようとすると、失敗してオブジェクト参照エラーが発生します。

渡された webdavpath からオブジェクトを作成する VBScript GetObject と同じように機能するクラスまたはメソッドはありますか、または誰かがこれに関するサンプル コードを提供できますか。

4

2 に答える 2

2

I think your problem is here:

keyword = engine.GetObject(Constants.WEBDAV_UKEN_PUB) as Keyword;

You are using the WEBDav URL of a publication, and then attempting a dynamic cast to Keyword. You can't cast a Publication to a Keyword, so the cast fails and your keyword variable is assigned null.

Using dynamic casts in this way is an easy way to fool yourself. The "As" keyword (C# keyword not Tridion keyword) should be used when you don't know at compile time what type you expect. If you know that you expect an item of type Keyword, then you should write:

keyword = (Keyword)engine.GetObject(Constants.WEBDAV_UKEN_PUB);

This way - when the cast fails, you'll get an exception that identifies the problem correctly.

于 2011-10-12T18:49:47.150 に答える
1

TOM.NET では、オブジェクトを取得して、それを読み取るパブを指定することはできません。コンテキスト内になるように TcmUri を変更する必要があります。

そう:

Repository context = (Repository)session.GetObject(WEBDAV_UKEN_PUB);
TcmUri keywordInContext = new TcmUri(keyword.Id.ItemId, keyword.Id.ItemType, context.Id.ItemId);
Keyword keyword = (Keyword)session.GetObject(keywordInContext);
于 2011-10-12T04:20:51.693 に答える