Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Anders Hattestad
Jun 19, 2011
  6641
(1 votes)

How to change context menu in the edit mode tree view

imageIf one would like to change the right right click context menu, to add one or more options there are several different approaches one could take. One that I like is to attach myself to the PageSetup event. This event will trigger on all pages that inherit from PageBase. That includes most of the admin and edit mode pages. (if not all)

Code Snippet
  1. public static void Initialize(int bitflags)
  2. {
  3.     EPiServer.PageBase.PageSetup += new EPiServer.PageSetupEventHandler(PageBase_PageSetup);
  4. }

 

if the page is EditTree we can then try to attach to the Menu object that will provide the right context menu. That property is protected, so we have to use reflection:

Code Snippet
  1. PropertyInfo info = type.GetProperty("Menu",BindingFlags.Instance|BindingFlags.NonPublic);
  2. RightClickMenu menu = info.GetValue(sender, null) as RightClickMenu;

The whole code will look like this

Code Snippet
  1. static void sender_PreRender(object sender, EventArgs e)
  2. {
  3.     if (sender is EditTree)
  4.     {
  5.         //I'm in right context menu in edit mode
  6.         Type type = typeof(EditTree);
  7.         string str = (sender as EditTree).ResolveUrlFromUI("edit/");
  8.         string imageThemeUrl = (sender as EditTree).GetImageThemeUrl("Tools/");
  9.  
  10.         PropertyInfo info = type.GetProperty("Menu",BindingFlags.Instance|BindingFlags.NonPublic);
  11.         RightClickMenu menu = info.GetValue(sender, null) as RightClickMenu;
  12.         RightClickMenuItem item = new RightClickMenuItem("Archive",
  13.             null,
  14.             CreateMenuAction("deletepage", "/custom/AdminPages/Delete2Archive.aspx", ""), "AllowDelete()",
  15.             imageThemeUrl + "Delete.gif",
  16.             RightClickMode.All);
  17.  
  18.         menu.Add("MyItem",item);
  19.     }
  20.     else
  21.     {
  22.         //I'm in view mode
  23.         if ((sender as EPiServer.PageBase).ContextMenu != null && (sender as EPiServer.PageBase).ContextMenu.Menu != null)
  24.         {
  25.             //(sender as EPiServer.PageBase).ContextMenu.Menu.Add("MyItem", EPiServer.Security.AccessLevel.Edit, new EPiServer.RightClickMenuItem("My Script", "MyScript()", "MyScriptSubMenu"));
  26.         }
  27.     }
  28. }
  29. private static string CreateMenuAction(string action, string url, string data)
  30. {
  31.     return string.Format("OnContextMenuAction(\"{0}\", \"{1}\", \"{2}\")", action, url, data);
  32. }

In my code, I want the Archive option enabled just as the Delete option. If you want more logic when its enabled just create yourself a java script function and provide the java script name in the constructor of the RightClickMenuItem.

Jun 19, 2011

Comments

Jun 19, 2011 08:42 PM

Nice one, I have just done a similar thing with my PageTypeExtensions project but this was using a PageAdapter. I wasn't aware of the PageSetup event and that you could modify the edit/admin pages in this way :)

Andreas Olsen
Andreas Olsen Aug 8, 2011 04:36 PM

Im Trying to use the example, creating my own panel page.

Do i have to inherit something special for the page Delete2Archive.aspx.
I have created a simple aspx page. It is displayed fine, but when it shows up, the tree view does not work anymore.
Im using the newpage action.

RightClickMenuItem item = new RightClickMenuItem("Archive",
null,
CreateMenuAction("newpage", "/PlugIn/CopyLanguage.aspx", ""), "true",
imageThemeUrl + "New.gif",
RightClickMode.All);


I have tried to inherit from Episerver.UI.Edit.NewPage, SystemBasePage,PageBase and System.Web.UI.Page

Could you include the Delete2Archive.aspx and the cs file also or tell me if there is something im missing.


Anders Hattestad
Anders Hattestad Aug 8, 2011 07:04 PM

her is the Delete2Archive.aspx file

http://world.episerver.com/Code/Anders-Hattestad1/How-to-change-context-menu-in-the-edit-mode-tree-view/

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog