Removing context menu item


I have previously successfully removed items from the edit panel / page context menu. Now I'm trying to do it for the EditTree context menu but the items I remove somehow magically reappear. I've been digging around in reflector but can't figure out where they come from.

I have used the mapping VPP to redirect the EditTree.aspx to use my custom implementation. The original EditTree.aspx inherits EPiServer.UI.Edit.EditTree while I have the hierarchy CustomEditTree.aspx : MyLib.EditTree : EPiServer.UI.Edit.EditTree. Note that CustomEditTree.aspx doesn't have a codebehind, MyLib.EditTree is a class sitting in a different project, if that could matter.

MyLib.EditTree contains this:

protected override void OnPreRender(EventArgs e)

    // Remove change access rights menu item
    if (Menu != null && Menu.Items.Contains("setaccesscaption"))

I can step through the code and see that base.OnPreRender results in 10 items being added to Menu.Items (it is empty before this call). I can also see that the "setaccesscaption" does exist and that it is remove, leaving 9 items when the method returns.

But still, all ten items are rendered in the menu. Even if I do Clear() on the collection and verify that it is indeed empty, the context menu will appear the same.

Any hints or suggestions?

Thanks in advance,

Sep 03, 2010 7:47

You're calling base.OnPreRender() on the EditTree, which calls RegisterRightClickScripts() which in turn unfortunately calls Render() on the RightClickMenu.

One way could be to do a public new RightClickMenuItemCollection Items { get; } and fiddle around with it a bit.

Sep 07, 2010 14:31

Yeah, i just saw the Render inside the RegisterRightClickScripts. I've been digging around trying to do it on the client but I couldn't there either. Could you elaborate on your idea with the Items collection? Seems like a good idea, but I don't see how I could get the RegisterRightClickScripts method to behave differently with it.

Sep 07, 2010 14:49

I was thinking something like this:

Subclass both EditTree and RightClickMenu, then modify your EditTrees Menu property to return your own implementation of RightClickMenu.

Modify your own implementation of RightClickMenu to exclude "setaccesscaption" (or a list of strings from a nice property :)) from Items.

I haven't tried it, but from looking through Reflector it should work. The alternative would be lots of copy and paste coding, I think. :(

Sep 07, 2010 15:01

Had only the RegisterRightClickScripts method used the Menu property and not the _menu field... And RegisterRightClickScripts is itself of course private. How could they make something so simple so hard? :) I guess i could subclass EditTree and divert the call to RegisterRightClickScripts to a method of my own, but I'll have a hard time with the base.OnPreRender call...

Sep 07, 2010 15:08

What if you also modify the constructor to initialize _menu to your implementation of RightClickMenu?

And I agree, this feels like a very hacky way to do it... :)

Sep 07, 2010 15:11

I can't do that from a subclass because _menu is private.

Sep 07, 2010 15:12

You certainly can with reflection!

Type t = typeof(EditTree);
FieldInfo field = t.GetField("_menu", BindingFlags.NonPublic); 
field.SetValue(t, new SuperDuperMenu()); 

Type t = this.GetType();
FieldInfo field = t.GetField("_menu", BindingFlags.NonPublic); 
field.SetValue(t, new SuperDuperMenu()); 

You might have to change the BindingFlags.

Sep 07, 2010 15:25

Yeah, but that's a reeeeeally nasty hack :) I might have to though, thanks for the suggestion!

Sep 07, 2010 15:28

Bleh, it will work as long as they don't change the name of the private field... :) Good luck!

Sep 07, 2010 15:30

Anyone got this working? I'm going insane here!!

Oct 11, 2012 10:55