Get Block which is rendering another block

Vote:
 

So I have blocks within block. What I want is to get a property set on the parent block to be availble on the child block. Is it possible to do?

[ContentType(DisplayName = "SubscriptionParentBlock", GUID = "56af697c-57a4-4599-a517-bb2e114b10bc", Description = "")]
    public class SubscriptionParentBlock : BlockData
    {
        [CultureSpecific]
        [Display(
            Name = "Heading",
            Description = "Main heading for the subscription.",
            GroupName = SystemTabNames.Content,
            Order = 10
        )]
        public virtual String Heading { get; set; }


        [Display(
    GroupName = SystemTabNames.Content,
    Name = "Background Color",
    Order = 11)]
        [ClientEditor(ClientEditingClass = "dijit/ColorPalette")]
        public virtual string TextBackgroundColor { get; set; }


        [Display(
        GroupName = SystemTabNames.Content,
        Order = 12)]
        [AllowedTypes(AllowedTypes = new[] { typeof(SubscriptionSubBlock) })]
        public virtual ContentArea SubBlocks { get; set; }

    }

The above one is my Parent Block and its TextBackgroundColor property I want in the child block which is like this

    [ContentType(DisplayName = "SubscriptionSubBlock", GUID = "19ee687e-a5e1-4f76-967e-6db3e73cee05", Description = "")]
    public class SubscriptionSubBlock : BlockData
    {

        [CultureSpecific]
        [Display(
            Name = "Heading",
            Description = "Sub heading for the subscription.",
            GroupName = SystemTabNames.Content,
            Order = 10
        )]
        public virtual String Heading { get; set; }


            [Display(
    GroupName = SystemTabNames.Content,
    Order = 320)]
            public virtual ContentArea ProductsBlock { get; set; }
    }

Is there a way around it?

#187225
Jan 16, 2018 12:54
Vote:
 

Hi Chaudhry,

It's possible to load references to a Block using the IContentSoftLinkRepository.Load method. If you pass your SubscriptionSubBlock's content reference, you'll get a list of SoftLink instances back where the ReferencedContentLink property will be your SubscriptionSubBlock's content reference. You may need to filter this list (on SoftLinkType, or through another mechanism) if you are referencing this block elsewhere. From there, you'd just load the OwnerContentLink via IContentLoader.Get<T> in order to retrieve your SubscriptionParentBlock's TextBackgroundColor property. Keep in mind, IContentSoftLinkRepository isn't wrapped in the same level of caching that IContentLoader features, so you may want to consider the performance implications of this before going down this route.

Alternatively, you could use Episerver Find to search for SubscriptionParentBlock instances and filter on those whose SubBlocks property contains your content reference. See: https://world.episerver.com/forum/developer-forum/EPiServer-Search/Thread-Container/2014/12/filtering-by-contentreferences-in-contentarea/

If you still have the opportunity to adjust your data model, you could also consider holding a single ContentReference to the SubscriptionParentBlock on your SubscriptionSubBlock instead of the current ContentArea approach, or consider using Pages to store this data to take advantage of their hierarchical nature (assuming each Sub block only has one parent).

Hope this helps,

/Matt

#187258
Edited, Jan 17, 2018 5:48
Vote:
 

Dejan Caric also blogged about IContentSoftLinkRepository for something similar, and it appears you can also use IContentRepository.GetReferencesToContent. Performance implications still apply (not a cached call).

See: https://www.dcaric.com/blog/episerver-9-references-to-content

#187259
Edited, Jan 17, 2018 6:01
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.