Block ContentReference ID?


So i have got my block instance (my block inherits from BlockData). So is it not possible to get the content ID?

On PageData (which i guess is equiliant to BlockData for Pages) i have my PageLink:PageReference, why is there no such thing on BlockData?

Jul 16, 2013 11:52

That's an interesting one! they are equivalent insofar as PageData is the data carrier for pages and BlockData is the data carrier for blocks, and they both inherit from ContentData. They also both have a ContentReference property, it's just that because of the way that EPiServer instantiates blocks, you won't see it on the BlockData class. You have to use a trick to get it :)

To explain further... for the PageData class, the content link (the page reference in old-speak) is stored in a property called 'PageLink'. This is then exposed to you for use in the PageData class, and hence in your page types.

For blocks, there is no PageLink property. This inherits from ContentData which, itself, has no inherent understanding of a ContentLink. That's an interesting design decision by the core team at EPiServer, but it probably makes sense. After all, some content may not have a ContentLink - it's an abstract concept after all. I might have expected to see it in an interface, but it's not there either. In fact, the ContentLink for blocks is added at the proxying stage. EPiServer uses the Castle proxying library to create a proxy class wrapper around the BlockData classes - or, more specifically - your block types. For this reason, you won't see the ContentLink on any class in your code because the proxies are created at runtime.

If you really really DO need to get the ContentLink of a block you can do so though, you just need to use reflection to grab it from the proxy. Here is an example of how you could grab that from the current block:

var contentLink = CurrentBlock.GetType().GetProperty("ContentLink").GetValue(CurrentBlock, null) as EPiServer.Core.ContentReference;

Hope this helps explain things!


Jul 16, 2013 12:48

A shared block instance is a Proxy class inheriting from your BlockData type and implementing IContent.

If you cast CurrentBlock to IContent you will be able to access data from the IContent Interface.

Read more about the IContent Interface at

Maybe you can find some inspiration on Johan Björnfot's blog:

Jul 16, 2013 14:01

Good point Alf - much simpler code. It seems counter-intuitive as the block in code doesn't inherit from IContent - it's only at runtime when the proxy is involved - but you are of course absolutely correct;

var contentLink = (CurrentBlock as IContent).ContentLink;


Jul 16, 2013 14:46

It all makes perfect sense now :)

Casting the proxy object (currentBlock) to IContent did the trick.

Thanks to both of you.

Jul 16, 2013 15:02
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.