London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!
AI OnAI Off
London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!
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?