4

We are developing a .net based CT based on Broker query Mechanism (filter):

ComponentPresentationAssembler  cpAssembler = new ComponentPresentationAssembler(Page ID,Page object);

In order to pass the page ID, I need to get the access of page on which the component is present. How can I access the page from package? Since this a CT, a component object would be available in page and not a page object. Tried the following piece of code, but without success:

string pageURI = _package.GetValue("Page.ID");
Page objPage = (Page)_engine.GetSession().GetObject(pageURI);

This is not working as there is no page object. What are the alternatives so that we can access the parent page of component from CT?

Huston Lopes
  • 622
  • 4
  • 17

2 Answers2

5

To address this I created an AddPageToComponentPresentation TBB. Here's the code:

using System;
using System.Collections.Generic;
using System.Text;

using Tridion.ContentManager;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;

namespace Tridion.Extensions.ContentManager.Templating {
    [TcmTemplateTitle("Add Page To ComponentPresentation")]
    class AddPageToComponentPresentation : TemplateBase {
        public override void Transform(Engine engine, Package package) {
            if (engine.PublishingContext.RenderContext.ContextItem != null) {
                Item pageItem = package.CreateTridionItem(ContentType.Page, engine.PublishingContext.RenderContext.ContextItem.Id);
                package.PushItem("Page", pageItem);
                Logger.Debug("Page Item added to Package");
            } else {
                Logger.Debug("No Context Item found");
            }
        }
    }
}
Jeremy Grand-Scrutton
  • 2,802
  • 14
  • 20
  • Nice solution, but now you have to be careful with all other TBBs, as it is fairly common to determine if a TBB is running in a Component Template context by checking if there is a Page object available in the package, the absence of it usually tells you you are in a CT while the existence could then tell you you are in a PT. – Bart Koopman Sep 24 '12 at 10:02
  • 1
    I never saw that check myself (not that that's indicative of anything) but it would have influence in that situation. I guess an option would be to change the name of the Item that get's created. "ContextPage" instead maybe. – Jeremy Grand-Scrutton Sep 24 '12 at 10:17
-1

Thanks a lot Jeremy. I tried this code, but was not able to use the interface TemplateBase

With hints from your code, I tried

Page page = _engine.PublishingContext.RenderContext.ContextItem as Page;

and this worked well. Also I could get publication object as:

Publication pub = (Publication)page.ContextRepository;

Thank you very much.

Huston Lopes
  • 622
  • 4
  • 17
  • 2
    The TemplateBase interface is part of the templating base project: http://sdltridionworld.com/community/extension_overview/templatingbase.aspx – Puntero Sep 24 '12 at 11:39
  • Oops, sorry. I guess, in the absence of the TemplateBase class that should probably be ITemplate instead ;) – Jeremy Grand-Scrutton Sep 24 '12 at 14:29