November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
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!
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 http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/46be3813-ce19-929e-93fc-be5356058967
Maybe you can find some inspiration on Johan Björnfot's blog:
http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/11/Shared-blocks--IContent/
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;
It all makes perfect sense now :)
Casting the proxy object (currentBlock) to IContent did the trick.
Thanks to both of you.
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?