How to create a new layer

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

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

Here's my code (based from selected answer here http://stackoverflow.com/questions/11816256/orchard-cms-adding-current-widget-types-to-custom-module-migration):

ContentDefinitionManager.AlterTypeDefinition("NavigationProfileMenuItem",
                cfg => cfg
                        .WithPart("MenuPart")
                        .WithPart("CommonPart")
                        .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;
            _contentManager.Publish(menuWidget.ContentItem);
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.

Thanks,
Noli
May 7, 2013 at 11:06 AM
Okay.. I got this to work by adding this line:
_contentManager.Flush();

based from the code in WidgetsServiceTest class.

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

Thanks,
Noli
Coordinator
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.
Coordinator
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.
Coordinator
May 7, 2013 at 7:02 PM
Right, but if you can avoid it, such as in this case, you should, right?