Welcome to the navigation

Ipsum exercitation minim dolor laboris duis reprehenderit enim anim ad proident, sed ut aliquip magna excepteur deserunt nulla aute velit sit eiusmod id amet, sunt. Consectetur do dolore amet, quis ea ullamco cillum nulla tempor elit, nisi sunt ut officia culpa sit voluptate occaecat ut dolor et ex dolor sint

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