World is now on Opti ID! Learn more

Anders Hattestad
Jun 20, 2011
  4884
(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
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |