Search code examples
tridiontridion-2011

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

Solution

  • 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");