Take the community feedback survey now.

Anders Hattestad
Jun 20, 2011
  4892
(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 - Opticon London 2025

This installment of a day in the life of an Optimizely OMVP gives an in-depth coverage of my trip down to London to attend Opticon London 2025 held...

Graham Carr | Oct 2, 2025

Optimizely Web Experimentation Using Real-Time Segments: A Step-by-Step Guide

  Introduction Personalization has become de facto standard for any digital channel to improve the user's engagement KPI’s.  Personalization uses...

Ratish | Oct 1, 2025 |

Trigger DXP Warmup Locally to Catch Bugs & Performance Issues Early

Here’s our documentation on warmup in DXP : 🔗 https://docs.developers.optimizely.com/digital-experience-platform/docs/warming-up-sites What I didn...

dada | Sep 29, 2025

Creating Opal Tools for Stott Robots Handler

This summer, the Netcel Development team and I took part in Optimizely’s Opal Hackathon. The challenge from Optimizely was to extend Opal’s abiliti...

Mark Stott | Sep 28, 2025

Integrating Commerce Search v3 (Vertex AI) with Optimizely Configured Commerce

Introduction This blog provides a technical guide for integrating Commerce Search v3, which leverages Google Cloud's Vertex AI Search, into an...

Vaibhav | Sep 27, 2025

A day in the life of an Optimizely MVP - Opti Graph Extensions add-on v1.0.0 released

I am pleased to announce that the official v1.0.0 of the Opti Graph Extensions add-on has now been released and is generally available. Refer to my...

Graham Carr | Sep 25, 2025