Adding items to EPiServer’s global menu in edit/admin mode

We needed to create a custom toolbox in EPiServer‘s edit mode as a central place of keeping additional functionality for a client’s 7.5 website. The global menu seemed like an excellent place to do this. For a brief description on how to get EPiServer’s global menu on your own pages see this previous article. Here is a short note on how it can be done; if you would like to add language support to the menu item labels, it would be a simple matter of getting hold of EPiServer’s LocalizationService through the ServiceLocator, or via constructor injection.

[MenuProvider]
public class ToolboxMenuProvider : IMenuProvider
{
  public IEnumerable<MenuItem> GetMenuItems()
  {
    // The main menu (label, path)
    var toolbox = new SectionMenuItem("Toolbox", "/global/toolbox")
      {
        IsAvailable = (request) => PrincipalInfo.HasEditAccess
      };

    // First submenu (label, path, target url)
    var tool1 = new UrlMenuItem("tool 1", "/global/toolbox/firsttool", "/toolbox/firsttool")
      {
        IsAvailable = (request) => PrincipalInfo.HasAdminAccess
      };

    // Second submenu (label, path, target url)
    var tool2 = new UrlMenuItem("tool 2", "/global/toolbox/secondtool", "/toolbox/secondtool")
      {
        IsAvailable = (request) => PrincipalInfo.HasEditAccess
      };

   return new MenuItem[] { toolbox, tool1, tool2 };
  }
}

Since we wanted to increse security for the tools we placed them under a _protected directory, and once we upgraded to MVC 5.2.2 we used RoutePrefix and Route to set up the target URLs.

[Authorize(Roles = "Administrators, WebAdmins, WebEditors")]
[RoutePrefix("toolbox")]
public class FirstToolController : Controller
{
  // ..
  [Route("firsttool")]
  public ActionResult Index()
  {
    // ..
    var model = SomeModel();
    return View("~/_protected/Toolbox/FirstTool/FirstTool.cshtml", model);
  }

One Response

  1. Björn Ali Göransson November 1, 2015