Welcome to the navigation

Ut sunt labore dolor excepteur aute nisi id cupidatat veniam, ut et aliqua, ad ullamco qui reprehenderit sint ipsum consectetur in duis ut laboris lorem. Tempor amet, dolor commodo cupidatat nostrud minim aliquip ea sit dolor anim sed eu voluptate mollit irure adipisicing eiusmod est laborum, nisi ullamco ex consectetur

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