Welcome to the navigation

In ut adipisicing minim et sint pariatur, sed duis lorem excepteur irure amet, esse voluptate aliquip sit proident, occaecat ut fugiat culpa laboris non anim. Ut tempor ut sit pariatur, enim exercitation aute cupidatat ex aliquip dolore non irure nisi minim labore in eu nulla excepteur esse ipsum consequat, dolor

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

Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author