GetDescendants by PageType


Hi Guys,

i want to search a subtree for pages of a specific pageType. I have a method for doing this, but i dont know if it's the best way of doing it performance wise..

var repo = ServiceLocator.Current.GetInstance();
repo.GetDescendents(Model.CurrentPage.ContentLink).Where(p => repo.Get(p) is Education).Select(repo.Get)

So i want to get all the descendants of the currentpage of PageType Education..

Is this the best way of doing it?

May 13, 2015 10:40

P.S using the latest EPiServer 8 CMS

Edited, May 13, 2015 10:40

You can use either GetDescendents or fetch child pages recursively.

What's faster... depends...

In some scenarios, I had better performance with GetDescendents, and better performance with recursive methods in other secanrios.
You should measure performance and see what's best for your use-case. dotTrace or even Stopwatch class would do the trick :)

The best performance-wise solution would be to use EPiFind or some other search technology, so that you don't have to hit the EPi database.

May 13, 2015 11:31

Thanks Dejan for the insight... but i was wondering if my method of getting descendants of a specific pagetype is a good one.. Because in my mind i'm doing a lot of rep.Get<>() method calls and maybe there is a better way of getting descendants of type T .. maybe a extension method.

May 13, 2015 11:57

Hi Vishal,

Methods of the IContentRepository are being cached.

May 15, 2015 8:37

Thanks Patrick for the insight ... then i'll keep it as is :-)

May 15, 2015 8:40

Another way is to use the IPageCriteriaQueryService.FindPagesWithCriteria.

// Type specified through page type ID
        private IEnumerable<TPageType> FindPagesByPageTypeRecursively<TPageType>(PageReference pageLink) where TPageType : IContent 
            var criteria = new PropertyCriteriaCollection
                new PropertyCriteria
                    Name = "PageTypeID",
                    Type = PropertyDataType.PageType,
                    Condition = CompareCondition.Equal,
                    Value = _contentTypeRepository.Load<TPageType>().ID.ToString(CultureInfo.InvariantCulture)

            return _pageCriteriaQueryService.FindPagesWithCriteria(pageLink, criteria).Cast<TPageType>().ToList();
May 19, 2015 9:59

An alternative is to use IContentModelUsage.ListContentOfContentType which can give you refrerences to all content items of a certain type. This call is not cached meaning it will always cause a database roundtrip, however in most cases I would prefer it before using GetDecendents and GetContent (which on one hand is cached but on the other hand risks a whole lot more database calls and gives a much larger memory footprint).

However ListContentOfContentType will give you all references regardless of location. To get only the references that are part of a sub tree you could use IContentLoader.GetAncestors to see which lies in the subtree.

May 20, 2015 13:20