This project is read-only.

How to build a 1.4.1 menu from the Recipe

Topics: General, Troubleshooting
Mar 21, 2012 at 4:15 PM
Edited Mar 21, 2012 at 4:20 PM



Two Part Question:

We had a working version of a recipe until we upgraded to Orchard 1.4 and Advanced Menu 1.4.1 yesterday

Our previous developer had added some command handlers to the previous advanced menu to allow us to create menus and menu items from the recipe

for example

      menu create "studentsite"
      simplemenuitem create "studentsite" "Student Dashboard" /Position:"0" /Url:"Dashboard/StudentContact" /Classes:"RecentActivity"
      simplemenuitem create "studentsite" "Attendance" /Position:"1" /Url:"Attendance/Calendar" /Classes:"Attendance"
      simplemenuitem create "studentsite" "Discipline" /Position:"2" /Url:"Discipline" /Classes:"Discipline"
      simplemenuitem create "studentsite" "Events" /Position:"6.3" /Url:"SchoolInfo/Events" /Classes:"School-Events"

      menuwidget create StyledMenuWidget /MenuName:studentsite /MenuStyle:"SRBVertical" /Title:"Site Menu Widget" /Zone:"AsideFirst" /Position:"2" /Layer:"Default" /Identity:"SiteMenuWidget"

I've gotten this recipe to run without errors now after some tweaking, but it appears that with Advanced menu 1.4.1 it no longer renders my menu items.I just get the default "Home" button in my menu, but oddly when I go to the menu in the dashboard I see my items in editor view..just not when it renders the actual menu. I did notice that there is a default Home item added in the "Enabled" event of Events.cs....

Question 1: Any ideas on how we can make this work?
Question 2: Should we be doing this differently somehow? I notice that there are no commands in the Advanced menu for creating menu items so I'm left to wonder if there's another/better way to do it.
What is see on the Orchard Dashboard image


From Events.cs in the Advanced menu  _mce_src=image
The command handlers from the previous version of the Advanced menu that we added (1.2 I believe) 

 /// <summary>
        /// menu create
        /// </summary>
        /// <param name="name"></param>
        /// <example>menu create "site" /Owner:"admin"</example>
        [CommandName("menu create")]
        [CommandHelp("menu create <name> [/Owner:<owner>]\r\n\t" + "Creates a new etif advanced menu")]
        public void CreateMenu(string name)
            // Initialize the owner string
            if (String.IsNullOrEmpty(Owner))
                Owner = _siteService.GetSiteSettings().SuperUser;
            var owner = _membershipService.GetUser(Owner);

            //if (!_orchardServices.Authorizer.Authorize(Permissions.EditMenuItems, T("Not allowed to create new menus")))
            //    throw new ApplicationException("Not allowed to create new menus");

            var newMenu = _orchardServices.ContentManager.New<AdvancedMenuPart>("Menu");
            newMenu.As<AdvancedMenuPart>().Name = name;
            newMenu.As<ICommonPart>().Owner = owner;

            catch (Exception ex)
                throw new ApplicationException("Menu (" + name + ") creation failed! \r\n" + ex.Message);

            Context.Output.WriteLine(T("Advanced menu created successfully.").Text);

 /// <summary>
        /// simplemenuitem create
        /// </summary>
        /// <param name="menuName"></param>
        /// <param name="menuItemName"></param>
        /// <example>
        /// simplemenuitem create "site" "Parents" OR
        /// simplemenuitem create "site" "Parents" /SubTitle:"" /Position:"1" /Url:"parents" /Classes:"" 
        /// </example>
        [CommandName("simplemenuitem create")]
        [CommandHelp("simplemenuitem create <menuName> <menuItemName> /Position:<position> [/Url:<url>] [/SubTitle:<subTitle>] [/Classes:<classes>] \r\n\t" + "Creates a new etif simple menu item")]
        public void CreateMenuItem(string menuName, string menuItemName)
            // Initialize empty parameter strings
            if (String.IsNullOrEmpty(Position)) Position = "1";
            if (Url == null) Url = menuItemName;

            if (String.IsNullOrEmpty(Owner))
                Owner = _siteService.GetSiteSettings().SuperUser;
            var owner = _membershipService.GetUser(Owner);

            // Create a new simple menu item and initialize with passed values
            var menuItem = _orchardServices.ContentManager.New<AdvancedMenuItemPart>("SimpleMenuItem");
            menuItem.MenuName = menuName;
            menuItem.Text = menuItemName;
            menuItem.SubTitle = SubTitle;
            menuItem.Position = Position;
            menuItem.Url = Url;
            menuItem.Classes = Classes;
            menuItem.DisplayText = true;
            menuItem.DisplayHref = true;
            menuItem.As<ICommonPart>().Owner = owner;


            Context.Output.WriteLine(T("Success!  A simple menu item was created for the " + menuName + " menu.").Text);
Mar 21, 2012 at 8:40 PM

I did find a bit of new information about this problem just now. 

I attach my debugger and step through the recipe steps until I get to the step which calls 

 menuwidget create StyledMenuWidget /MenuStyle:"SRBVertical" /MenuName:"studentsite" /Title:"Site Menu Widget" /Zone:"AsideFirst" /Position:"2" /Layer:"Default" /Identity:"SiteMenuWidget"

In the Szmyd.Orchard.Modules.Menu.Commands.MenuCommands code, I can step through to the point where it creates the widget and the MenuName is properly set


However when I look at the WidgetPart records it creates the widgets having the name "main"


When I run through the rest of my recipe, the other menu widget also gets created as "main"

If I manually change the menu names in the database to what they are supposed to be, my menu shows up....


Mar 23, 2012 at 9:58 AM

Not sure why I didn't notice earlier, but there is an error on line 90 of (missing !)


It should read    

widget.As<MenuWidgetPart>().MenuName = !string.IsNullOrWhiteSpace(MenuName) ? MenuName : "main";

I will raise this as an issue

Mar 25, 2012 at 1:11 AM

Thanks! I'll look into that and try to help you out after the weekend.