Get Block's Page

Vote:
 

Hey all,

is there a quick and nice or at least any way to get Block's Parent Page?

Let's say I have some Page which has some Block in some of it's ContentArea. How could I get that Page from outside Block's Model/Controller?

Ideally I would like to have a Property - ParentPage - on some Blocks, so I could the access some properties of that ParentPage when used in Block's view... does it makes sense?

CMS version is 11 something...

Thank you!

#220549
Apr 02, 2020 11:09
Vote:
 

Not tested but something like this 

            Injected<IPageRouteHelper>  pageRouteHelper;


	    if (pageRouteHelper.Page is BasePageData currentPage)
            {
               var property = currentPage.Property
            }
#220551
Apr 02, 2020 12:09
Vote:
 

So do you want to use the properties of the current page(not sure about parent page) on the block?

#220552
Apr 02, 2020 12:09
Vote:
 

Hi Ričardas,

You can try this extension method to retrieve the Ancestor and Self-content.

public static IEnumerable<IContent> GetAncestorsAndSelf(this IContentLoader contentLoader, IContent content, bool includeEntries = false)
{
            if (contentLoader == null || content == null)
                return Enumerable.Empty<IContent>();

            var ancestorsAndSelf = contentLoader.GetAncestors(content.ContentLink)?.Reverse().ToList() ?? new List<IContent>();

            if (includeEntries && content is IContent entry)
            {
                var ancestorEntriesAndSelf = contentLoader.GetAncestorEntriesAndSelf(entry);
                ancestorsAndSelf.AddRange(ancestorEntriesAndSelf);
            }
            else
            {
                ancestorsAndSelf.Add(content);
            }

            return ancestorsAndSelf;
}
#220562
Edited, Apr 02, 2020 19:24
* 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.