Custom web templates and confusing Managed Navigation

Custom web templates and confusing Managed Navigation

As you might have seen when you create a new site collection or sub site in SharePoint 2013 based on an out of the box web template the navigation settings are defaulted to be of type Managed Navigation. Now if you create a custom web template you have the option to configure some of the navigation settings declaratively, something you can do since SharePoint 2007. A nice post about that about most of the options can be found at Controlling navigation options from the onet.xml by Vesa Juvonen. I also posted about these properties a while ago in my post about Slidedeck SharePoint Connections 2013 Navigation in a Multi Site Collection environment being available. However no matter what you will configure declaratively you will always end up with the managed navigation selected when using web templates created for SharePoint 2013.

When fiddling around with this issue I ended up looking in the feature responsible for setting these properties. The feature called Portal Navigation Properties will call an feature receiver, the Microsoft.SharePoint.Publishing.NavigationFeatureHandler. You can check that out using reflector and will find  that it will call a ApplyNavigationProperties method that is responsible for setting the correct properties. The funny thing is that after setting all the properties from XML the following snippet is called

publishingWeb.SetBooleanProperty("NavigationPropertiesSet", true); publishingWeb.Update(); if (publishingWeb.Web.Site.CompatibilityLevel >= 15) { FriendlyUrlUtilities.ActivateManagedNavigation(publishingWeb.Web); }

As you can imagine the ActivateManagedNavigation will activate the Managed Navigation. So whatever configuration you will do declaratively will be ignored if you are running a SharePoint 2013 site. So having found this method it did make sense in why the navigation was always set to Managed Navigation on newly created sub sites. If you would Bing or Google for the issue you will find some blogs like Navigation Settings in SharePoint 2013 Site Definitions that point out that you can use a custom receiver to fix the issue by resetting the navigation to use the PortalProvider as a source:

WebNavigationSettings navigationSettings = new WebNavigationSettings(web);
navigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
navigationSettings.Update();
web.Update();

This does make sense and yet has only minor downside, by applying this on your web templates you can no longer set inheritance of the navigation on your sub sites. If you have a web template that you apply to your site collection, and use the same template to create some sub sites on that site by activating the sample above will result in all the sub sites having custom navigation settings and there will be no inheritance. As this is a unintended side effect in most cases you can simply check if the given web is a rootweb before setting the navigation settings.

if (web.IsRootWeb)
{
    WebNavigationSettings navigationSettings = new WebNavigationSettings(web);
    navigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
    navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
    navigationSettings.Update();
    web.Update();
}

By doing so you will keep inheritance from you parent web as the ApplyNavigationProperties will set inheritance for you as you can see in the following sample.

if (web.IsRootWeb)
{
 // apply managed navigation
}
else
{
    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
    webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
}

As you can see setting the navigation settings in a web template can be a bit tedious the first time you create your web template for SharePoint 2013, but once you know what the default behavior is it is fairly easy to work with it. Just make write some logic to reset the settings if they don’t fit your requirements. eep in mind though that Microsoft is trying to push is towards a Managed Navigation structure so you might want to reconsider that as a path the explore before not using it and writing logic to turn it off.

Leave a Reply