Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 24, 2010 @ 12:18
    Hendy Racher
    1

    Removing Context Menu Items

    Hi

    Is it possible to remove all context menu items and add only those required, rather than explicitly declare all the unnecessary actions ?

    private void BaseTree_BeforeNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
    {
    if (node.NodeType == "content") // be nice if this returned docType alias
    {
    if (node.Icon == "comment.png") // quickest way of finding a comment node ! (rather than lookup from node.NodeID)
    {
    node.Menu.Clear(); // seems to stop node from being rendered
    node.Menu.Add(ActionDelete.Instance);

    //node.Menu.Remove(ActionNew.Instance);
    //..
    //..
    //..
    }
    }
    }

    Also refering to the WIKI page, it seems as if node.NodeType should return the docTypeAlias, but it always seems to return 'content', is this expected ? (have also tried the BaseContentTree.BeforeNodeRender event)

    TIA for any suggestions,

    Hendy


  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 24, 2010 @ 12:33
    Hendy Racher
    1

    Hi,

    The following seems to work (leaving the refresh action):

      node.Menu.RemoveRange(0, node.Menu.Count - 1);
      node.Menu.Add(ActionDelete.Instance);

    Still curious as to the best way of checking the docTypeAlias of the current node.

    TIA,

    Hendy

     

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Nov 24, 2010 @ 12:42
    Richard Soeteman
    1

    Hi Hendy,

    Nice one that RemoveRange. There is no better option to determine the doctype than determine it based on its icon or create a document object and check it by its contentType. Wouldn't recommend the last option on large tree structures since that can cause a performance issues.

    Waht you could do is rename the icons to a unique filename and make sure nobody can change it. Still I agree that it should be better to have the  DocumenttypeAlias being included in the node object or in the Eventrguments.

    Cheers,

    Richard

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 24, 2010 @ 12:55
    Hendy Racher
    1

    Hi Richard,

    Thanks for confirming that, I'll update the WIKI.

    There's the node.TreeType property that also returns 'content', so would have thought node.NodeType would be a sensible place to return the docTypeAlias, although appreciate that could break existing code.

    Thanks,

    Hendy

  • Funka! 398 posts 661 karma points
    Oct 03, 2012 @ 00:27
    Funka!
    0

    I followed the advice in the wiki (it actually had an error in it that i fixed) and things work as expected when i right-click on my admin tree, ... but now am getting NullReferenceException error when spawning a Content Picker. Here's what is coming back from the TreeDataService.ashx file...

    [NullReferenceException: Object reference not set to an instance of an object.]
       MyCustomAppBase.BaseTree_BeforeNodeRender(XmlTree& sender, XmlTreeNode& node, EventArgs e) +150
       umbraco.cms.presentation.Trees.BeforeNodeRenderEventHandler.Invoke(XmlTree& sender, XmlTreeNode& node, EventArgs e) +0
       umbraco.cms.presentation.Trees.BaseTree.OnBeforeNodeRender(XmlTree& sender, XmlTreeNode& node, EventArgs e) +30
       umbraco.cms.presentation.Trees.BaseContentTree.Render(XmlTree& Tree) +191
       umbraco.loadContent.Render(XmlTree& tree) +18
       umbraco.presentation.webservices.TreeDataService.LoadTree(TreeRequestParams treeParams) +77
       umbraco.presentation.webservices.TreeDataService.GetXmlTree() +140
       umbraco.presentation.webservices.TreeDataService.ProcessRequest(HttpContext context) +55
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

    Has anyone had any recent experience working with this, or have any advice for something i might need to do differently? I am thinking I might need to adjust my code to first see if something called ActionDelete.Instance actually exists in node.Menu before just assuming to call Remove?

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Oct 03, 2012 @ 08:21
    Richard Soeteman
    1

    Check if node.Menu not is null. If it's null it's a treepicker and then you shouldn't remove the context menu's since there is no menu.

    Hope this helps,

    Richard

  • Funka! 398 posts 661 karma points
    Oct 04, 2012 @ 20:05
    Funka!
    0

    Thank you, Richard!  I tested this and you are exactly right. 

    I went ahead and updated the wiki page again with this fix and a note.  On that same topic, as you'll see I also added to the bottom of the wiki page, is a note about ApplicationBase being deprecated in favor of umbraco.businesslogic.ApplicationStartupHandler, according the note in the source code? It seems they are mostly interchangeable from my own limited interaction, but for posterity and for anyone looking to do more with their menus, looking into these differences would probably be advised.

    Thanks to everyone for this golden thread!

  • Robert Dolman 22 posts 42 karma points
    Dec 05, 2012 @ 12:08
    Robert Dolman
    0

    I know this is quite an old thread but I kept coming back here when looking for the answer to my question: how can I completely hide a menu item programmatically. The code above states;

    node.Menu.Clear();// seems to stop node from being rendered

    This didn't work for me using 4.11.1, but I found the following works just fine;

    private void BaseTree_BeforeNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e) {
        node = null;
    }
  • Funka! 398 posts 661 karma points
    Dec 05, 2012 @ 21:03
    Funka!
    0

    Just to clarify for posterity, Robert, ... are you trying to hide the entire node, the entire context menu, or just an item in the context menu? The code snippet you posted seems like it will enitrely destroy all trees, correct?

  • David Armitage 508 posts 2078 karma points
    May 26, 2017 @ 10:10
    David Armitage
    1

    Hi,

    I know this task is been open a while but for anyone looking to remove menu items in Umbraco 7 then here it is.

    private void ContentTreeController_MenuRendering(TreeControllerBase sender, MenuRenderingEventArgs e)
            {
                //remove unwanted menu items
                e.Menu.Items.Remove(e.Menu.Items.Where(x => x.Alias == "notify").FirstOrDefault());
                e.Menu.Items.Remove(e.Menu.Items.Where(x => x.Alias == "sendToTranslate").FirstOrDefault());
                e.Menu.Items.Remove(e.Menu.Items.Where(x => x.Alias == "rollback").FirstOrDefault());
                e.Menu.Items.Remove(e.Menu.Items.Where(x => x.Alias == "auditTrail").FirstOrDefault());
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft