Block ContentReference ID?

pme
pme
Vote:
 

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?

#73359
Jul 16, 2013 11:52
Vote:
 

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!

 

#73364
Jul 16, 2013 12:48
Vote:
 

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/

#73369
Jul 16, 2013 14:01
Vote:
 

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;

 

#73370
Jul 16, 2013 14:46
pme
Vote:
 

It all makes perfect sense now :)

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

Thanks to both of you.

#73371
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.