Try our conversational search powered by Generative AI!

Obtaining the ID of a parent block from within the child block


Hi All,

I've been reading various posts on this forum in an attempt to figure out a way to obtain the ID of a block's parent, however most of these posts appear to be for EPiServer 6 & 7. I am working with EPiServer DXC and am trying to get the ID of the block's parent to then pass into my view as a value to be assigned to a data attribute.

I have seen several pieces of advice against looking outside of the scope of the block, however in my specific case, I can't seem to find another way to achieve this.

I am currently using the foundation.js "sticky" element, and to anchor it to another element I require the ID of said element. However, this has to be in place before the DOM is readied, I have attempted (successfully) to retrieve and insert the correct ID via JS, after the DOM is ready and while the property correctly updated, the item refused to "stick"

My current workaround is to get the user who creates the block in the CMS to enter the numerical part of the ID of the parent into a field, which is then passed through to the view on page build and while it works, I am aware this is by no means best practice and is more of a test to confirm that the ID can be provided by the Model.

In an attempt to retrieve this ID programmatically, I have used the following code:

var anchorid = "block" + (Model as EPiServer.Core.IContent).ParentLink.ID;

However it returns the ID of the "block" view folder. I found that I could decrement the retrieved value to 1 to get the ID of the parent, but this only applies in the case that the block sits within the parent's content folder, not elsewhere.

Below is the HTML code that I am using to create the sticky block:

<div class="inner" style="padding:0px;">
    <div class="cell" data-sticky-container>
        <div class="sticky" data-sticky data-top-anchor="@anchorid" data-btm-anchor="@botAnchor" data-margin-top="3.25" data-sticky-on="small">
            <img class="image1" src="@Url.ContentUrl(Model.HeadingImage)" alt="@altText" />

Is there any way that I can get the ID of the parent block and insert it into the "data-top-anchor" attribute before the DOM is readied?

Nov 08, 2019 12:27

The same block may be added to multiple pages, so a block does not necessarily have one single parent.

Nov 08, 2019 12:43

Hi Tomas,

Thanks for the advice, I am aware of this being the case, which is why my current solution won't work, as it only works in the case that it has a single parent. I've been trying (and failing) to reference the block's parent regardless of where it sits and what page it is currently sitting on.

I was reading about several other people trying to access information about content outside of the scope of their block and how it is generally advised against, but in the case of foundation sticky, I cannot seem to find any other way to achieve this.


Nov 08, 2019 12:52

You could (I'm not saying you should) do this, to get all «parents» of the block with ID = 42.

  FROM [dbo].[tblContentSoftlink]
  WHERE ContentLink = '42'

The database table tblContentSoftlink is used to keep track of links, used both for the link validation job, and also text «Changes made here will affect at least 2 items» I see when I edit a block.

A better approach that direct database queries would be using IContentSoftLinkRepository.

Edited, Nov 08, 2019 13:04

Could you not pass the ID to the block controller through viewdata? In the view for the page when calling PropertyFor you will know the ID of that page - you can then read it in via ParentActionViewContext.

This similar post may help :

Nov 08, 2019 15:45
* 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.