Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to find current item ID in the template

In my C# or dreamweaver template I need to know what am I rendering. The problem is that I don't know for sure if I'm looking for a page or component. I could probably use package.GetByType(ContentType.Page) and if it's empty - get content of a component, but I feel there should be a shorter way.

Example of David is shorter:

engine.PublishingContext.ResolvedItem.Item.Id
like image 917
Andrey Marchuk Avatar asked Feb 11 '13 09:02

Andrey Marchuk


1 Answers

engine.PublishingContext.ResolvedItem.Item.Id

You can also check the Publishing Context's resolved Item and see if it's a Page or not (if it's not, then it's a Component).

For example:

Item currentItem;
if (engine.PublishingContext.ResolvedItem.Item is Page)
{
    currentItem = package.GetByName(Package.PageName);
}
else
{
    currentItem = package.GetByName(Package.ComponentName);
}
TcmUri currentId = engine.GetObject(currentItem).Id;

If you want to shortcut the engine.GetObject() call, then you may be able to get the ID from the Item's XML directly:

String currentId = currentItem.GetAsSource().GetValue("ID");
like image 63
David Forster Avatar answered Oct 02 '22 07:10

David Forster