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);
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)
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.
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.
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?
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.
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;
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?
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 ?
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
Hi,
The following seems to work (leaving the refresh action):
Still curious as to the best way of checking the docTypeAlias of the current node.
TIA,
Hendy
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
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
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...
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?
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
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!
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;
This didn't work for me using 4.11.1, but I found the following works just fine;
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?
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.
is working on a reply...