SaaS CMS has officially launched! Learn more now.

How to get the Parent ContentReference from an existing ContentReference


Hi guys,

I have a ContentReference object, which refers to a page in my content tree, I want to get the ContentReference of the parent node?

How can I do this?

Jun 07, 2018 15:37

You have to get the page out from the IContentLoader first. You can load it out as a PageData or IContent object then use the ParentLink property which will be the parent.

Jun 07, 2018 16:11

Example although you usually want the IContentLoader to be injected either in the constructor or using the Injected<> method.

var pageReference = ContentReference.EmptyReference;
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var page = contentLoader.Get<IContent>(pageReference);
var parentPage = contentLoader.Get<IContent>(page.ParentLink);

Here I've created a pageReference empty reference but that should be the reference of the page you're trying to get the parent of.

Edited, Jun 07, 2018 16:15

An alternative is to use EPiServer.IContentLoader.GetAncestors(ContentReference contentLink) then will you get all ancestors for a specific content item or EPiServer.Web.Routing.AncestorReferencesLoader.GetAncestors(ContentReference contentLink) if you only want the references to the ancestors.

Jun 08, 2018 10:03
* 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.