A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Anders Hattestad
Jun 20, 2011
  4907
(0 votes)

Solution to many children in edit modes page tree

There was a forum post some days ago, where the question was how to manage 1000+ child's under a single parent by Deane Barker.

I have since that post been thinking about how it’s possible to change how the edit mode page tree is rendered.

First I though about control adaptors, but it’s not possible to override methods on the target control, but only to add logic in the OnInit, OnLoad etc..

But a hidden pearl provided me with a solution: TagMapping.

TagMapping is a method where you can override a web control to use another web control in the web.config

just like this:

Code Snippet
  1. <location path="EPiServer/UI">
  2.   <system.web>
  3.     <httpRuntime maxRequestLength="1000000" />
  4.     <pages enableEventValidation="true">
  5.       <tagMapping>
  6.         <add tagType="EPiServer.UI.WebControls.PageTreeView" mappedTagType="IteraFun.EPiServerOverride.PageTreeViewOwn"/>
  7.       </tagMapping>
  8.       <controls>
  9.         <add tagPrefix="EPiServerUI" namespace="EPiServer.UI.WebControls" assembly="EPiServer.UI" />
this is extremely powerful, and I have not known about this before. With one line in the web.config its possible to override a web control with your own class.

The class I wanted to override was PageTreeView. That is a class with some overrides like GetData based on a viewPath. First I override that method like this:

Code Snippet
  1. protected override HierarchicalDataSourceView GetData(string viewPath)
  2. {
  3.     if (viewPath.EndsWith("All"))
  4.         (this.DataSource as PageDataSource).PageLoader.GetChildrenCallback = GetRest;
  5.     else
  6.         (this.DataSource as PageDataSource).PageLoader.GetChildrenCallback = GetMax10;
  7.     var result = base.GetData(viewPath.Replace("All", ""));
  8.     return result;
  9. }
  10.  
  11. private PageDataCollection GetMax10(PageReference pageLink)
  12. {
  13.  
  14.     var pages = DataFactory.Instance.GetChildren(pageLink);
  15.     if (pages.Count > 10)
  16.     {
  17.         var count = pages.Count;
  18.         pages.RemoveRange(10, pages.Count - 10);
  19.         var page = EPiServer.DataFactory.Instance.GetPage(pageLink);
  20.         page = page.CreateWritableClone();
  21.         page.PageName = "Show rest 11-" + count;
  22.         page.ParentLink = pageLink;
  23.         pages.Add(page);
  24.     }
  25.     return pages;
  26. }
  27. private PageDataCollection GetRest(PageReference pageLink)
  28. {
  29.     var pages = DataFactory.Instance.GetChildren(pageLink);
  30.     if (pages.Count > 10)
  31.     {
  32.         var count = pages.Count;
  33.         pages.RemoveRange(0, 10);
  34.     }
  35.     return pages;
  36. }
If the viewPath contains All, I will show the rests of the pages, else I will show 10 pages, and a show rest node.

this will display something like this:

image

Then the tricky part is to change how the last node is rendered.

PageTreeView implements ICallbackEventHandler, and I have to override those. These are not virtual, but using the new statement it’s possible to change it’s behavior.

I copied the base code from ILSpy, and discovered of course a lot of private and even internal methods. No worry thou, that was expected.

I made myself a healer method

Code Snippet
  1. public object DoMethod(string name, object[] parameters)
  2. {
  3.     var method = typeof(PageTreeView).GetMethod(name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  4.     return method.Invoke(this, parameters);
  5. }

that took care of that problem Smile

Then I rewrote the GetCallbackResult() like this

Code Snippet
  1. string _eventArgument;
  2. public new void RaiseCallbackEvent(string eventArgument)
  3. {
  4.     this._eventArgument = eventArgument;
  5. }
  6. public new string GetCallbackResult()
  7. {
  8.     string eventArgument = this._eventArgument;
  9.     StringBuilder stringBuilder = null;
  10.     if (!string.IsNullOrEmpty(eventArgument))
  11.     {
  12.         PageTreeNode treeNode = null;
  13.         PageTreeView.CallbackArgument callbackArgument = new PageTreeView.CallbackArgument(eventArgument);
  14.         if (callbackArgument.Action.Equals("populate"))
  15.         {
  16.             DoMethod("PopulateFromViewPath",new object[] {callbackArgument.DataPath, 0});
  17.         }
  18.         else
  19.         {
  20.             if (callbackArgument.Action.Equals("select"))
  21.             {
  22.                 //this.PopulateRecursive(callbackArgument.DataPath);
  23.                 DoMethod("PopulateRecursive", new object[] { callbackArgument.DataPath });
  24.             }
  25.             else
  26.             {
  27.                 if (callbackArgument.Action.Equals("update"))
  28.                 {
  29.                     //treeNode = this.LoadTreeNode(callbackArgument.DataPath);
  30.                     treeNode = DoMethod("LoadTreeNode",new object[] { callbackArgument.DataPath}) as PageTreeNode;
  31.                 }
  32.             }
  33.         }
  34.         string text = string.Empty;
  35.         try
  36.         {
  37.             text = callbackArgument.ContextNodeId.Substring(this.ClientID.Length, callbackArgument.ContextNodeId.IndexOf('_', this.ClientID.Length) - this.ClientID.Length);
  38.         }
  39.         catch
  40.         {
  41.         }
  42.         if (!string.IsNullOrEmpty(text))
  43.         {
  44.             foreach (PageTreeNode pageTreeNode in this.Nodes)
  45.             {
  46.                 if ((pageTreeNode.DataItem as PageData).PageLink.ID == (pageTreeNode.DataItem as PageData).ParentLink.ID)
  47.                 {
  48.                     var propInfo = typeof(PageTreeNode).GetProperty("DataPath", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  49.                     propInfo.SetValue(pageTreeNode, (pageTreeNode.DataItem as PageData).PageLink.ID + "All", null);
  50.                         
  51.                            
  52.                 }
  53.                 typeof(PageTreeNode).GetProperty("RootIdentifier", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(pageTreeNode, text, null);
  54.                 //pageTreeNode.RootIdentifier = text;
  55.             }
  56.         }
  57.         base.ClearChildState();
  58.         //this.CreateChildControlsFromItems(true);
  59.         DoMethod("CreateChildControlsFromItems", new object[] { true });
  60.         stringBuilder = new StringBuilder();
  61.         HtmlTextWriter htmlTextWriter = new HtmlTextWriter(new StringWriter(stringBuilder));
  62.         //this.RenderCallbackContents(htmlTextWriter, treeNode);
  63.         DoMethod("RenderCallbackContents", new object[] { htmlTextWriter, treeNode });
  64.         htmlTextWriter.Close();
  65.     }
  66.     this.Page.Response.ContentType = "text/plain";
  67.     if (stringBuilder == null)
  68.     {
  69.         return string.Empty;
  70.     }
  71.     return stringBuilder.ToString();
  72. }

The node image Is the same PageData as the parent node, and I did a check if the ParentPageLink is the same as PageLink, and if it was I change the DataPath

Code Snippet
  1. foreach (PageTreeNode pageTreeNode in this.Nodes)
  2. {
  3.     if ((pageTreeNode.DataItem as PageData).PageLink.ID == (pageTreeNode.DataItem as PageData).ParentLink.ID)
  4.     {
  5.         var propInfo = typeof(PageTreeNode).GetProperty("DataPath", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  6.         propInfo.SetValue(pageTreeNode, (pageTreeNode.DataItem as PageData).PageLink.ID + "All", null);
  7.     }
  8.     typeof(PageTreeNode).GetProperty("RootIdentifier", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(pageTreeNode, text, null);
  9.     //pageTreeNode.RootIdentifier = text;
  10. }

So when I expand the Show rest node now, the datapath is set to the parentNodes PageReference ID and added All

This works like a charm,

image

 

This logic could easy be adapted to show

pages 1-10
pages 11-20
pages 21-30
pages 31-40
pages 41-48, but my code here only shows the rest Smile

The only problem is that if you are in view mode on a page that is hidden, that page will not be visible in the edit tree. This is fixable to turn of this feature when one enters edit mode. It seems like the viewpath is empty when that happens, so I changed the GetData like this

Code Snippet
  1. protected override HierarchicalDataSourceView GetData(string viewPath)
  2. {
  3.     if (viewPath=="")
  4.         (this.DataSource as PageDataSource).PageLoader.GetChildrenCallback = GetChildrenDefault;
  5.     else if (viewPath.EndsWith("All"))
  6.         (this.DataSource as PageDataSource).PageLoader.GetChildrenCallback = GetRest;
  7.     else
  8.         (this.DataSource as PageDataSource).PageLoader.GetChildrenCallback = GetMax10;
  9.     var result = base.GetData(viewPath.Replace("All", ""));
  10.     return result;
  11. }

I’m not sure if this change to the edit tree is a smart thing to do, but I’m more excited about the TagMapping feature. That technic is certainly a great method to extend a already extendable CMS like EPiServer even further.

Code can be found here

Jun 20, 2011

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP: Learning Optimizely Just Got Easier: Introducing the Optimizely Learning Centre

On the back of my last post about the Opti Graph Learning Centre, I am now happy to announce a revamped interactive learning platform that makes...

Graham Carr | Jan 31, 2026

Scheduled job for deleting content types and all related content

In my previous blog post which was about getting an overview of your sites content https://world.optimizely.com/blogs/Per-Nergard/Dates/2026/1/sche...

Per Nergård (MVP) | Jan 30, 2026

Working With Applications in Optimizely CMS 13

💡 Note:  The following content has been written based on Optimizely CMS 13 Preview 2 and may not accurately reflect the final release version. As...

Mark Stott | Jan 30, 2026

Experimentation at Speed Using Optimizely Opal and Web Experimentation

If you are working in experimentation, you will know that speed matters. The quicker you can go from idea to implementation, the faster you can...

Minesh Shah (Netcel) | Jan 30, 2026

How to run Optimizely CMS on VS Code Dev Containers

VS Code Dev Containers is an extension that allows you to use a Docker container as a full-featured development environment. Instead of installing...

Daniel Halse | Jan 30, 2026

A day in the life of an Optimizely OMVP: Introducing Optimizely Graph Learning Centre Beta: Master GraphQL for Content Delivery

GraphQL is transforming how developers query and deliver content from Optimizely CMS. But let's be honest—there's a learning curve. Between...

Graham Carr | Jan 30, 2026