Read properties of the parent Block

Vote:
 

I have a block in the content area of another block.

How can I access one of the properties of the parent block from the view?

I am using episerver 7.5

#114192
Dec 05, 2014 14:39
Vote:
 

I would not recommend that a block tries to access information outside its context.

Could it be possible for you to push the content from the wrapping View by using RenderSettings (WebForms) or AdditionalViewData (MVC)?

#114200
Edited, Dec 05, 2014 15:50
Vote:
 

Can you describe more your case? Maybe something interesting comes in mind..

#114350
Dec 10, 2014 14:34
Vote:
 

I am trying to achive the same thing, why isn't it recommended?

This will only generate an error due to the ParentLink is not refering to an object implementing IContent nor IContentData...

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var data = contentRepository.Get<IContentData>(((IContent)this).ParentLink);

"this" represents the block context, and of course it is pretty obvious it will throw an exception but is there a way i can do this?

Best regards,

Hampus

#133732
Edited, Sep 09, 2015 18:14
Vote:
 

ParentLink in this case is the parent in the content tree, not the information about where it's placed in a ContentArea.

#133733
Sep 09, 2015 19:13
Vote:
 

Thanks for your answer Alf.

So in other words, there is no way to get the parent relativley from the current block (if it's decendant of course).
Is there no relation between the child and the parent exposed on the object then?

BR

#133734
Sep 09, 2015 20:53
Vote:
 

No "official" way that I have seen and what I've seen so far I would not recommend that you try to look for information outside the scope of the block. Mainly because there are multiple ways to use a block:

  • ContentReference
  • IList<ContentReference>
  • ContentArea
  • Block as a Property

And some other ways that I might miss right now.

#133735
Sep 09, 2015 21:17
Vote:
 

You should look at your block as isolated unit of stuff that may render some other stuff. I would agree with Alf, if this block needs information from hosting party (another's block content area, page content area, etc) then host is responsible for providing that data and not block accessing this data and pulling in from somewhere. This sounds more like violating responsobility boundaries of components..

#133736
Sep 09, 2015 21:32
Vote:
 

Thanks for the input, I totally agree with you both after looking it through a second time, I didn't really take the time to understand the true nature of blocks.
I solved it in another way instead, which doens't violate the responsibility, I also got the chance to refactor my previous implementation.

Cheers.

BR

#133775
Sep 10, 2015 9:16
Vote:
 

It's technically possible via ContentAreaRenderer (google for content area extensibility), but not recommended.

#133777
Sep 10, 2015 9:53
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.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.