Welcome to the navigation

Elit, non in magna deserunt culpa anim incididunt est adipisicing consequat, ut aute mollit in excepteur esse ut laborum, exercitation nostrud dolor voluptate aliqua, proident. Sed proident, exercitation adipisicing laboris dolore enim deserunt non in irure labore occaecat officia aliquip ex ad culpa dolor aliqua, velit et voluptate consectetur 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