Tuesday, October 13, 2015

EpiServer - Find Any Descendants Of <T> (Type Generic)

Easy To use
  public static IList<T> GetDescendants<T>(this ContentReference pageLink, ILanguageSelector languageSelector) where T : PageData  
     {  
       if (pageLink.CompareToIgnoreWorkID(ContentReference.RootPage))  
       {  
         throw new NotSupportedException("The root page cannot be converted to type " + typeof(T).Name);  
       }  
       IList<T> descendants = new List<T>();  
       var descendantsReferences = ContentRepository.GetDescendents(pageLink).ToList();  
       if (!descendantsReferences.IsNullOrEmpty())  
       {  
         foreach (var pageRef in descendantsReferences)  
         {  
           var page = languageSelector == null ? pageRef.ToPageReference().GetPage<T>() : pageRef.ToPageReference().GetPage<T>(languageSelector);  
           if (page != null)  
             descendants.Add(page);  
         }  
       }  
       return descendants;  
     }  

  

IEnumerable<PageData> test5 = _repo.GetChildren<PageData>(ContentReference.RootPage);  
       foreach (PageData pageData in test5)  
       {  
         FindDescendantsOfType<PuffPage>(pageData, descendantsType);  
       }

  private static void FindDescendantsOfType<T>(PageData page, ICollection<T> descendantsType) where T : class  
     {  
       var children = ServiceLocator.Current.GetInstance<IContentLoader>().GetChildren<PageData>(page.PageLink);  
       foreach (var child in children)  
       {  
         if (child is T)  
         {  
           descendants.Add(child as T);  
         }  
         FindDescendantsOfType(child, descendants);  
       }  
     }  

Get Children Of Type


 public static IEnumerable<SitePageData> GetChildrenOfType<T>(SitePageData currentPage, ContentReference topNode)  
       where T : SitePageData  
     {  
       var pages = ContentLoader.GetChildren<T>(topNode);  
       return FilterForVisitor  
         .Filter(pages)  
         .OfType<SitePageData>()  
         .Where(x => x.IsVisibleOnSite() && x.VisibleInMenu);  
     }  

Get Items Inkluding Top node


 public static List<NavigationItem> GetNavigationItemsInkludingTopNode(SitePageData currentPage, ContentReference topNode)   
     {  
       var topPage = ContentLoader.Get<SitePageData>(topNode);  
       var topLevelPages = GetChildrenOfType<SitePageData>(currentPage, topNode);  
       var pages = topLevelPages.ToList();  
       pages.Insert(0,topPage);  
       return GetNavItemList(pages, currentPage).ToList();  
     }  



 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);
            }
        }

No comments:

Post a Comment