This project is read-only.

How to create a new layer

Topics: Customizing Orchard
May 7, 2013 at 10:57 AM

I'm trying to create a widget and put it in a new layer.

Here's my code (based from selected answer here

                cfg => cfg
                        .DisplayedAs("Profile Menu Item")
                        .WithSetting("Description", "Injects Profile Types as menu items")
                        .WithSetting("Stereotype", "MenuItem")

            var menuItem = _contentManager.Create("NavigationProfileMenuItem");
            var menu = _menuService.Create("Profile Menu");
            var menuPart = menuItem.As<MenuPart>();
            menuPart.Menu = menu;
            menuPart.MenuPosition = "0";

            _widgetsService.CreateLayer("ProfileLayer", "ProfileLayer", "true");
           var profileLayer = _widgetsService.GetLayers().FirstOrDefault(x => x.Name == "ProfileLayer");           
            var widget = _widgetsService.CreateWidget(profileLayer.Id, "MenuWidget", "Profile Menu Widget", "0", "BeforeContent");            
            widget.RenderTitle = false;
            var menuWidget = widget.As<MenuWidgetPart>();
            menuWidget.Record.Menu = menu.ContentItem.Record;
The above code fails when creating the layer (after creating the layer, it should be able to find the new layer, right?. But it returns null).
Please help.

May 7, 2013 at 11:06 AM
Okay.. I got this to work by adding this line:

based from the code in WidgetsServiceTest class.

Is this correct? and what's the use of the Flush() function?

May 7, 2013 at 6:11 PM
No, this is not the correct way: you should almost never have to call flush. CreateLayer returns a LayerPart, so you already have it and don't need to retrieve it.
May 7, 2013 at 7:00 PM
Well, in 1.6 there might be some reasons to call Flush, like doing queries on content which have been created in the current request.
May 7, 2013 at 7:02 PM
Right, but if you can avoid it, such as in this case, you should, right?