This Blog is not active anymore, further posts will be available on my company website. Read new posts here
In February I wrote this blogpost which describes the process of how to add a menu item to the context menu using the Umbraco V4 Event system. In UmbImport PRO I've used this mechanism. When I was testing this I came across a bug in my code when using a pagepicker, this was showing an empty tree in the node picker.
Then I got a little flashback to the level 2 course I attended last November where Niels told us to check if there was a menu attached to the node (which is not the case for the pagepicker) before adding menu items to it, otherwise an exception is thrown what will result in an empty tree. This is what happened in my case. In the example below I've only added node.Menu!= null check and now everything is working fine.
1: using System;
2: using umbraco.BusinessLogic;
3: using umbraco.cms.presentation.Trees;
4: using umbraco.interfaces;
5:
6: namespace UnpublishAction
7: {
8: /// <summary>
9: /// Add unpublish to the menu item
10: /// </summary>
11: public class AddUnpublishActionEvent :ApplicationBase
12: {
13: public AddUnpublishActionEvent()
14: {
15: BaseContentTree.BeforeNodeRender += new BaseTree.BeforeNodeRenderEventHandler(BaseTree_BeforeNodeRender);
16: }
17:
18: /// <summary>
19: /// Before a menu item gets rendered we will add the unpublish action if the document is published
20: /// </summary>
21: private void BaseTree_BeforeNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
22: {
23: ///Only unpublish when published
24: if (node.Menu!= null && !node.NotPublished.GetValueOrDefault(true))
25: {
26: //Find the publish action and add 1 for the index
27: int index = node.Menu.FindIndex(delegate(IAction a) { return a.Alias == "publish"; })+1;
28:
29: //Insert unpublish action
30: node.Menu.Insert(index, UnpublishAction.Instance);
31: }
32: }
33: }
34: }
35:
Download Source
Hope it didn't get you into trouble, sorry if it did.