SharePoint Tip of the Day : Programmatically rollback to structural navigation in SharePoint 2013


Managed Navigation are the default choice when you create a publishing site in SharePoint 2013 but what do you need to do if you want to rollback programmatically to the old fashion structural navigation ?

The Navigation settings for each site are stored within the the property bag of the SPWeb and you can access it through

SPWeb.AllProperties[“_webnavigationsettings”] or through SharePoint Manager like showed hereunder

_webnavigationsettings

There is a new object WebNavigationSettings from the Microsoft.SharePoint.Publishing.Navigation dll that is responsible for managing (update & persist) the xml settings within the property bag.

Application pages like /_layouts/15/AreaNavigationSettings.aspx are adapting their UI based on these settings and by creating a new WebNavigationSettings with the current web as parameter to the constructor.

To rollback to the structural navigation, you can do all required configuration or use the built-in ResetToDefaults() method that will put back the PortalProvider instead of the Taxonomy one.

// Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings
public void ResetToDefaults()
{
    base.RequireNotFrozen();
    this.siteMapProviderSettingsByName.Clear();
    if (this.isRootWeb)
    {
        this.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
        this.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
    }
    else
    {
        this.GlobalNavigation.Source = (this.GlobalNavigation.InheritPortalSettingsFromParent ? StandardNavigationSource.InheritFromParentWeb : StandardNavigationSource.PortalProvider);
        this.CurrentNavigation.Source = (this.CurrentNavigation.InheritPortalSettingsFromParent ? StandardNavigationSource.InheritFromParentWeb : StandardNavigationSource.PortalProvider);
    }
    this.AddNewPagesToNavigation = true;
    this.CreateFriendlyUrlsForNewPages = true;
}

 

So in code, once you know it, it’s only a matter of doing

WebNavigationSettings webNavigationSettings = new WebNavigationSettings(rootWeb);
webNavigationSettings.ResetToDefaults(); // will rollback to PortalProvider
webNavigationSettings.Update();

To adapt the property bag and get the expected behaviour.

And if you’re wondering why it is the case by default, it is because the Portal Navigation properties feature which is part of the site definition of any publishing site (and stappled to a few others)  (Feature  Id= “541F5F57-C847-4e16-B59A-B31E90E6F9EA”) has a receiver that will do parse the feature properties and then always call the FriendlyUrlUtilities if the version is superior or equal to SharePoint 2013.

                if (publishingWeb.Web.Site.CompatibilityLevel >15)
                {
                    FriendlyUrlUtilities.ActivateManagedNavigation(publishingWeb.Web);
                }

And you guess it, that ActivateManagedNavigation will work with the WebNavigationSettings object and adapt it this time to support the Taxonomy Navigation.

_webnavigationsettings2

So if you’re stapling some feature with some receiver to rollback to the old way of working with navigation (and you should if you’re targeting a SharePoint 2013 Standard License !) make sure that you run after that receiver or execute it through PowerShell once the site is provisioned.

 

One thought on “SharePoint Tip of the Day : Programmatically rollback to structural navigation in SharePoint 2013

Leave a reply to Alexis Cancel reply