Welcome to the navigation

Id dolore eu dolor ad incididunt adipisicing in ea amet, duis occaecat eiusmod ut reprehenderit ullamco voluptate ipsum elit, officia cupidatat consectetur nisi sit labore. Velit excepteur nostrud consequat, nulla veniam, ex quis do enim voluptate qui incididunt deserunt non magna minim ea dolore dolore ut in nisi in lorem

Yeah, this will be replaced... But please enjoy the search!

Programmatically "Hide in navigation" on page creation in EPiServer 9

Categories Tags

The "Display In Navigation" has been around for ages and was previously coupled to EPiServer's built-in menu controls. Today we typlically build our own implementations of menus since the requirements with mobile first and the demands on highly adaptable navigation is pretty much standard stuff.

Set default values

What we are looking for is the MetaDataProperty PageVisibleInMenu, these properties has typically always been badly covered in the documentation (i.e. http://world.episerver.com/documentation/class-library/?documentId=cms/7/e841b826-ef19-f96f-73dd-4cab2d9f4170). 

In EPiServer 8 and newer this setting can be modified on page creation in two different ways. All PageData objects implement the method SetDefaultValues, which in its turn comes from the ContentData object. What we need to do is to override this method and set our metadataproperty values in that method.

public class NewsPage
{
    public override void SetDefaultValues(ContentType contentType)
    {
        base.SetDefaultValues(contentType);

        // In EPi 8 and newer you can use the direct property VisibleInMenu 
        this.VisibleInMenu = false;
        
        // Some EPi 7 versions was a bit buggy sometimes, use the MetaDataProperty instead
        //this[MetaDataProperties.PageVisibleInMenu] = false;
    }
}

This will ensure your "Display in navigation" property is unchecked by default