Question: 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?

Answer: 
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](http://www.cronin.nl/weblog/context-bag-a-tridion-templating-pattern) 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.
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.