Get all pages of a particular Pagetype



I would like to get all the pages of a particular Page type under a parent node. So far I use this code:

 PageReference careersExplorerParent = new PageReference(GoConstruct.Properties.Settings.Default.CP_Profiles_ParentId);

    PageDataCollection profiles = DataFactory.Instance.GetChildren(careersExplorerParent);

    results = profiles.Where(child => child.PageTypeName == "CareerExplorerCareerProfilePage" && child.IsVisibleOnSite() && child.StopPublish > DateTime.Now && child.IsDeleted == false);

But it only bringing back the first child node pages. I would also like to bring back pages under the child - for example Grand Child.:

Top Node:
         CareerExplorerCareerProfilePage Page type (Child page)

              CareerExplorerCareerProfilePage Page type (Grand Parent)

I hope this makes sense,


Jun 14, 2016 13:09
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
// loading a block type
var contentType = contentTypeRepository.Load<CareerExplorerCareerProfilePage>();
// getting the list of block type usages, each usage object has properties ContentLink, LanguageBranch and Name
var usages = contentModelUsage.ListContentOfContentType(contentType);
// TODO: filter it by ancestors to find all below node
// TODO: cache if necessary for performance. Call to ListContentOfContentType is not cached by Episerver...
// TODO: use classes below Episerver.Filters to filter on access rights, publish status etc
Hope that helps!
Edited, Jun 14, 2016 13:25

Other alternatives:

create a recursive method, something like:

        protected static void GetDescendantsOfType<T>(PageData page, ICollection<T> descendants) where T : class
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var children = contentRepository.GetChildren<PageData>(page.ContentLink);
            foreach (var child in children)
                if (child is T)
                    descendants.Add(child as T);
                GetDescendantsOfType(child, descendants);

or use ContentRepository's GetDescendents and then filter by type etc:

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var descendants = contentRepository.GetDescendents(page.ContentLink);
Jun 14, 2016 16:19

I would recommend Daniels approach or to use FindPagesWithCriterion:

using getchildren or getdescendants requires the application to read the entire data structure which can be quite performance heavy.

Have a look at my blog post about good practices reading content from Epi

speciallt when considering cache and locks

Jun 15, 2016 12:02
* 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.