November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
In MVC there is a context item stored on the request, you can get it from:
requestContext.HttpContext.Items[ContentContext.ContentContextKey] as Stack<ContentPropertiesStack>
It basically is a stack where you can get information about nested parents and nested properties. So you can e.g. know when rendering a specific property if that property is nested within ContentAreas and/or nested in other properties (case when rendering block properties).
You should though be aware that for the information to be accurate you should either render through PropertyFor/BeginEditSection-EndEditSection. If you have custom rendering you need to call static Push/Pop methods on ContentContext when rendering a content/property to get correct info from class.
In WebForms you could traverse the Control collection upwards to find e.g. IContentSource implementations.
Be also aware that the above suggestions are kind of "internal" details and can be changed in future.
Marjia, i am trying to do exactly what you were asking, using Johan's resolved answer would you be able to provide a little more information on how you achieved this?
Regards
Minesh
Hi, Minesh,
As you can see, this is a quite old post, so something has probably changed in the meantime. Please first try to access the parent block using ParentLink:
public static IContent GetParent(this IContent content) { var parentContent = Get<IContent>(content.ParentLink); return parentContent; }
If that doesn't give you the proper IContent, then you might give it a try what Johan proposed. (But I think yourblock.GetParent() should work nowadays, I just don't have time to try it out). This is the method I used earlier (that was 7.0, so I think you won't need it, but here it is):
public int GetNoOfChildrenInRow(RequestContext requestContext) { var parentStack = requestContext.HttpContext.Items[ContentContext.ContentContextKey] as Stack<ContentContext.ContentPropertiesStack>; if (parentStack != null) { var parents = parentStack.ToArray(); foreach (var currentParent in parents) { var contentRepo = ServiceLocator.Current.GetInstance<IContentRepository>(); var parentBlock = contentRepo.Get<IContentData>(currentParent.ContentLink); var rowBlock = parentBlock as RowBlock; if (rowBlock != null) { return rowBlock.OneToFourBlocks.ContainsAnyBlocks() ? rowBlock.OneToFourBlocks.Count : 0; } } } return 0; }
Thanks all, this post answered my question of fetching a content parent, just thought i'd share the extension I ended up with:
public static ContentReference GetContentAreaParent(this HttpRequestBase request) { //Retrieve the Request's content stack and get the 2nd element without disturbing the stack. var contentstack = request?.RequestContext.HttpContext.Items[ContentContext.ContentContextKey] as Stack<ContentContext.ContentPropertiesStack>; if (contentstack?.Count > 1) { var parent = contentstack.ToArray()[1]; return parent.ContentLink; } return null; }
A reccomendation is to do a Peek() into the stack intead of a Pop() since otherwise you are likely to break other code that is dependent on the stack.
Thank you for the feedback Johan, glad I shared. Since I need the 2nd value I've copied the stack to an array.
From what I see, in the controller, I have CurrentBlock.Property.OwnerLink, which gives me the ID of the current block.
However,
gives me the parent folder in the block structure (the one seen on the right of edit mode, for example "Global Library", "Folder for general blocks", ...), not the page structure.
What I need to do is to access a property on the parent block.