Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.
Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.
Maybe you could simply iterate using ParentLink reference.
Here is the extension method snippet:
using System.Collections.Generic; using System.Linq; using EPiServer; using EPiServer.Core; namespace ContentAreaWithPreview.Business { public static class ContentLoaderExtensions { public static IEnumerable<IContent> GetAllParents(this IContentLoader contentLoader, ContentReference contentReference) { if (ContentReference.IsNullOrEmpty(contentReference)) { return Enumerable.Empty<IContent>(); } contentReference = contentLoader.Get<IContent>(contentReference).ParentLink; var contentReferences = new List<IContent>(); while (!ContentReference.IsNullOrEmpty(contentReference)) { var content = contentLoader.Get<IContent>(contentReference); contentReferences.Add(content); contentReference = content.ParentLink; } return contentReferences; } } }
and using the method in code:
ContentReference cr = new ContentReference(1234); var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); contentRepository.GetAllParents(cr); contentLoader.GetAllParents(cr);
You could also simply use GetAncestors:
IEnumerable<IContent> ancestors = contentRepository.GetAncestors(currentPage.ContentLink);
Edit: Gayathri beat me to it :-)
How do I get all the parents of my CurrentPage? How do I use GetParents with Model.CurrentPage?