Umbraco 7 - Create context menu item only for specific document type
Hi Guys,
So I am trying to figure out the best way to create context menu items in the back-end that's only displayed on a specific document type.
So Iv'e managed to do this Ok I just think there might be a better way.
The code below works and does add a context menu only on this specific document type. My question is....
Is there any way of doing this without using the content service to get the node just to check the document type.
eg. var content = contentService.GetById(nodeId);
It just seems overkill having to run this code everytime the user pulls up the context menus especially when the especially when this custom stuff isn't related to the other nodes. I've noticed it does slow the context menu loading down slightly which is annoying.
I am just wondering if there is any smart way of pulling the document type from the sender or something???
private void ContentTreeController_MenuRendering(TreeControllerBase sender, MenuRenderingEventArgs e)
{
var treeAlias = sender.TreeAlias;
var queryString = e.QueryStrings["id"];
if (e.NodeId != "-1")
{
var contentService = ApplicationContext.Current.Services.ContentService;
int nodeId;
if(int.TryParse(e.NodeId,out nodeId))
{
var content = contentService.GetById(nodeId);
if (content != null)
{
var nodeTypeAlias = content.ContentType.Alias;
if (nodeTypeAlias == "myNodeTypeAlias")
{
var validateMember = new MenuItem("approveMember", "Approve Member");
validateMember.AdditionalData.Add("actionView", "/App_Plugins/BackendExtensions/views/approveMember.html");
validateMember.Icon = "facebook-like";
e.Menu.Items.Insert(e.Menu.Items.Count, validateMember);
}
}
}
}
}
I hadn't thought of whether it would be possible to get the document type alias from the sender...
the sender inherits TreeControllerBase which in turn inherits UmbracoAuthorizedApiController
so in the past to do what you are trying to do (if the item you are trying to check the type of is published), I've used the sender to get an UmbracoHelper class to query the published cache, which is quicker than using the content service...
eg:
var contentItem = sender.Umbraco.TypedContent(e.NodeId);
However, if it were possible to read the document type from the sender then I think it would be via the PublishedContentRequest property:
var type = sender.UmbracoContext.PublishedContentRequest.PublishedContent.DocumentTypeAlias;
but I'm fairly sure in the context of tree menu rendering this won't be populated!!
Another possibility would be to add the item to a dictionary in memory each time the menu is executed, so at least repeated uses of the menu wouldn't be slow, looking first in this dictionary before using contentservice/contenthelper... to retrieve the alias.
Thanks for your reply. I will have another look and see what i can find. At least using the published content and storing this in memory will do the trick.
I will let you know if I manage to find the document type.
Umbraco 7 - Create context menu item only for specific document type
Hi Guys,
So I am trying to figure out the best way to create context menu items in the back-end that's only displayed on a specific document type.
So Iv'e managed to do this Ok I just think there might be a better way.
The code below works and does add a context menu only on this specific document type. My question is....
Is there any way of doing this without using the content service to get the node just to check the document type.
eg.
var content = contentService.GetById(nodeId);
It just seems overkill having to run this code everytime the user pulls up the context menus especially when the especially when this custom stuff isn't related to the other nodes. I've noticed it does slow the context menu loading down slightly which is annoying.
I am just wondering if there is any smart way of pulling the document type from the sender or something???
Thanks in advanced.
Kind Regards
David
Hi David
I hadn't thought of whether it would be possible to get the document type alias from the sender...
the sender inherits TreeControllerBase which in turn inherits UmbracoAuthorizedApiController
so in the past to do what you are trying to do (if the item you are trying to check the type of is published), I've used the sender to get an UmbracoHelper class to query the published cache, which is quicker than using the content service...
eg:
However, if it were possible to read the document type from the sender then I think it would be via the PublishedContentRequest property:
but I'm fairly sure in the context of tree menu rendering this won't be populated!!
Another possibility would be to add the item to a dictionary in memory each time the menu is executed, so at least repeated uses of the menu wouldn't be slow, looking first in this dictionary before using contentservice/contenthelper... to retrieve the alias.
regards
marc
Hi Marc,
Thanks for your reply. I will have another look and see what i can find. At least using the published content and storing this in memory will do the trick.
I will let you know if I manage to find the document type.
Kind Regards
David
is working on a reply...