• Publisert
  • 2 min

Extended PageTree

<p>If you want to extend EPiServer's PageTree to only/not include spesific pagetypes you can do this by creating your own pagetree control and make it inherite from EPiServer's PageTree class.</p>

EPiServer's PageTree class has a function called CreateItemRecursive and this is where you can add your logic to include or exclude elements from the tree.

In my example I added two properties to mye ExtendedPageTree control which takes in a comma seperated list of pagetypesnames that the user wants to filter on. Here is my example code for this:

 public class ExtendedPageTree : PageTree
    {
        public string IncludeOnlyPageTypeNames { get; set; }
        public string ExcludePageTypeNames { get; set; }

        protected override void CreateChildControls()
        {
            PageData page = PageReference.IsNullOrEmpty(RootLink) ? null : GetPage(RootLink);
            CreateTemplateControl(page, HeaderTemplate, 0, false);
            CreateItemsRecursive(1, string.Empty);
            CreateTemplateControl(page, FooterTemplate, 0, false);
        }

        private void CreateItemsRecursive(int level, string path)
        {
            PageHierarchicalEnumerable enumerable = PageLoader.HierarchicalSelect(path);
            if ((enumerable.Pages.Count != 0) && !PageReference.IsNullOrEmpty(RootLink))
            {
                PageHierarchyData data = (path != string.Empty)
                                             ? ((PageHierarchyData) enumerable.GetParent())
                                             : new PageHierarchyData(PageSource.GetPage(RootLink), PageLoader);
                CreateTemplateControl(data.Page, IndentTemplate, level, data.HasChildren);
                foreach (PageHierarchyData data2 in enumerable)
                {
                    PageData page = data2.Page;
                    if (!string.IsNullOrEmpty(ExcludePageTypeNames))
                    {
                        string[] pagetypenames = ExcludePageTypeNames.Split(',');
                        bool dontInclude = false;
                        foreach (string s in pagetypenames)
                        {
                            if (s.Equals(page.PageTypeName, StringComparison.CurrentCultureIgnoreCase))
                            {
                                dontInclude = true;
                                break;
                            }
                        }
                        if(dontInclude) continue;
                    }

                    if (!string.IsNullOrEmpty(IncludeOnlyPageTypeNames))
                    {
                        string[] pagetypenames = IncludeOnlyPageTypeNames.Split(',');
                        bool include = false;
                        foreach (string s in pagetypenames)
                        {
                            if (s.Equals(page.PageTypeName, StringComparison.CurrentCultureIgnoreCase))
                            {
                                include = true;
                                break;
                            }
                        }
                        if(!include) continue;
                    }

                    CreateTemplateControl(page, ItemHeaderTemplate, data2.Indent, data2.HasChildren);
                    CreateItemTemplateControl(page, level, data2.HasChildren,
                                              (CurrentPage == null)
                                                  ? PageReference.EmptyReference
                                                  : CurrentPage.PageLink);
                    if ((data2.HasChildren && ((NumberOfLevels == -1) || (level < NumberOfLevels))) &&
                        IsExpanded(data2.Page.PageLink))
                    {
                        CreateItemsRecursive(level + 1, page.PageLink.ToString());
                    }
                    CreateTemplateControl(page, ItemFooterTemplate, level, data2.HasChildren);
                }
                CreateTemplateControl(data.Page, UnindentTemplate, level, data.HasChildren);
            }
        }

        private void CreateItemTemplateControl(PageData page, int level, bool hasChildren, PageReference currentPageLink)
        {
            var template = new PageTemplateContainer(page, level, hasChildren);
            if (level == 1)
            {
                if (page.PageLink.CompareToIgnoreWorkID(currentPageLink) && IsExpanded(page.PageLink))
                {
                    InstantiateSelectedExpandedTopTemplate(template);
                }
                else if (IsExpanded(page.PageLink))
                {
                    InstantiateExpandedTopTemplate(template);
                }
                else if (page.PageLink.CompareToIgnoreWorkID(currentPageLink))
                {
                    InstantiateSelectedTopTemplate(template);
                }
                else
                {
                    InstantiateTopTemplate(template);
                }
            }
            else if (page.PageLink.CompareToIgnoreWorkID(currentPageLink) && IsExpanded(page.PageLink))
            {
                InstantiateSelectedExpandedItemTemplate(template);
            }
            else if (IsExpanded(page.PageLink))
            {
                InstantiateExpandedItemTemplate(template);
            }
            else if (page.PageLink.CompareToIgnoreWorkID(currentPageLink))
            {
                InstantiateSelectedItemTemplate(template);
            }
            else
            {
                InstantiateItemTemplate(template);
            }
            Controls.Add(template);
        }
    }