Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

How to get the Parent ContentReference from an existing ContentReference

Ayo
Ayo
Vote:
 

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?

#193918
Jun 07, 2018 15:37
Vote:
 

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.

#193924
Jun 07, 2018 16:11
Vote:
 

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.

#193925
Edited, Jun 07, 2018 16:15
Vote:
 

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.

#193945
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.