5

I want to fetch some page metadata field values in my CT, Template Building Block (Razor Template). The Component Template type is dynamic with a REL output format.

I want the metadata information only when someone publishs the page.

To get the page object I am using TridionHelper.Engine.PublishingContext.RenderContext.ContextItem as Page Or TridionHelper.Engine.PublishingContext.RenderContext.ContextItem.Id

The first one returns an empty/null value while the second one raises an error while publishing the page.

Does anyone have an idea of what might be going on?

  • 1
    you say that TridionHelper.Engine.PublishingContext.RenderContext.ContextItem returns null, do you get this null value when you actually publish a page, or do you see this when you preview it or run it in the template builder ? also, you say you are using dynamic component templates: is this component you are publishing added to a page (so are you publishing the page), or are you publishing the component only ? – Harald Hoffelinck Nov 4 '13 at 13:03
  • In my CT TBB itself, I have writeout the ContextItem once and then ContextItem.Id . With ContextItem, page published successfully but I didn't get anything on my page source or in that DCP output. With ContextItem.Id or ContextItem as page, page get failed while publishing. – Sourav Garg Nov 4 '13 at 13:08
14

In a Component Template TBB, you don't have (direct) access to the Page object. The property ContextItem would return the Component that is being rendered with the current Component Tempalte. Please check that, as I'm not familiar with what TridionHelper should do.

Hence your code ContextItem as Page will return null.

Back to your question -- how to pass metadata from Page Template to Component Template, you could do so using the ContextVariables, or more generically using Dominic's ContextBag pattern for Tridion. I would probably pass the entire Page object to the CT TBB.

Anyway, a more important factor to consider here is the question "why do you need Page level metadata in a Dynamic Component Presentation?". DCPs are published and potentially assembled on a Page (at request-time) independent of the Page. So potentially, a visitor would see the DCP on a completely different page (and in different context).

The decision shouldn't be taken lightly and I encourage you to think about it seriously. Most of the times you don't need (and you shouldn't have to make available) Page meta in DCPs, although there are some valid use cases. Please post your thoughts and what you are trying to achieve (maybe in a different question) and I'll try to guide you through that.

| improve this answer | |
  • Mihai is right. When you are using a DCP, "there is no page". Even if you place it on a page, the DCP will still be rendered without a page context. You can pass parameters via the component presentation assembler, but that's a different story entirely. – Dominic Cronin Nov 5 '13 at 12:41
4

I'm not sure why the contextitem would be null (i'll do some tests myself), but you could solve this the other way around. Instead of getting page metadata from a component template, you could put the required page metadata in the context variables You would put this code in a TBB on your page template (before you call your rendercomponentpresentations() off course

engine.PublishingContext.RenderContext.ContextVariables.Add("metadata", "metadatavalue");

Then, in your component tbb's, you could check iff the contextvariables contain a value. If you have too many metadata values to add to the contextvariables, you could also add the page's ID to a context variable, get that, and retrieve the page with that ID.

note that in your component template, you will still need to check whether the variable is present, you can't just assume it will be there.

| improve this answer | |
  • Currently my Page Template TBB's are in DWT so I can't do much in page TBB. – Sourav Garg Nov 4 '13 at 13:28
  • Your page template consists of a Dreamweaver template (DWT), but should be able to add more template building blocks to your page template. – Harald Hoffelinck Nov 4 '13 at 13:33
  • 1
    By the way, i did the following test - i created a new razor TBB, and put this in my component template (which is set up as a dynamic REL CT) - the razor TBB contains this piece of code @TridionHelper.Engine.PublishingContext.RenderContext.ContextItem.Id then, i put a new component on my page with my dynamic component template, and then preview the page, and the page id is outputted, so there must be something else in your code that's causing issues – Harald Hoffelinck Nov 4 '13 at 13:57
  • you got the ID on page preview or on published page source? – Sourav Garg Nov 4 '13 at 14:26
  • 1
    The error is caused because the contextitem object is null. Probably because we're dealing with a dynamic component presentation - it will be rendered in it's own context (outside of a page). – Harald Hoffelinck Nov 5 '13 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.