Try our conversational search powered by Generative AI!

GetDescendants by PageType

Vote:
 

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?

#121537
May 13, 2015 10:40
Vote:
 

P.S using the latest EPiServer 8 CMS

#121538
Edited, May 13, 2015 10:40
Vote:
 

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.

#121540
May 13, 2015 11:31
Vote:
 

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.

#121542
May 13, 2015 11:57
Vote:
 

Hi Vishal,

Methods of the IContentRepository are being cached.

#121607
May 15, 2015 8:37
Vote:
 

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

#121609
May 15, 2015 8:40
Vote:
 

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();
        }
#121867
May 19, 2015 9:59
Vote:
 

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.

#121913
May 20, 2015 13:20
* 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.