What is the easiest way to find the current item id in a template

In my C # or Dreamweaver template, I need to know what I'm doing. The problem is that I do not know for sure if I am looking for a page or component. I could use it package.GetByType(ContentType.Page), and if it's empty, get the contents of the component, but I believe there should be a shorter way.

David example is shorter:

engine.PublishingContext.ResolvedItem.Item.Id
+5
source share
2 answers
engine.PublishingContext.ResolvedItem.Item.Id

You can also check the permitted Publishing Context element and see if it is a page or not (if it is not, then it is a component).

For instance:

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 shorten the call to engine.GetObject (), you can directly get the identifier from the XML element:

String currentId = currentItem.GetAsSource().GetValue("ID");
+5
source

As I saw it before:

// Contains the call you describe in your question
Page page = GetPage(); 
if (page == null)
{
    // Contains a call using package.GetByName("Component") 
    // to avoid the situation with multiple Components on the package
    Component comp = GetComponent();
    // Do component stuff
}
else
{
    // Do page stuff
}

, , , .

+3

All Articles