Question

If a publishing site collection has managed or structural navigation set as the source for the top navigation, how do you work with these settings programmatically? The SPWeb.Navigation.TopNavigationBar property returns an entirely different set of navigation nodes (the Top Link Bar on non-publishing sites).

Here are some UI screenshots where the settings I'm talking about are located:

Not-Publishing (Top Link Bar Settings):

Top Link Bar Settings

Publishing (Global Navigation Settings):

Publishing Navigation Settings Global Navigation Settings

How can you programmatically detect whether a site is using the SPNavigation.TopNavigationBar, Managed Navigation, or Structural Navigation for its Global Navigation / top navigation settings?

Was it helpful?

Solution

Alright, so after peering into the inner workings of the code behind for those settings pages, I've found that you can detect the source of the publishing site's navigation by using the WebNavigationSettings class and reading its GlobalNavigation.Source property (a StandardNavigationSource enum) like such:

if (PublishingWeb.IsPublishingWeb(web))
{
    WebNavigationSettings settings = new WebNavigationSettings(web);
    switch (settings.GlobalNavigation.Source)
    {
        case StandardNavigationSource.PortalProvider:
            // Data source is Structured Navigation
            break;
        case StandardNavigationSource.TaxonomyProvider:
            // Data source is Managed Navigation
            break;
        case StandardNavigationSource.InheritFromParentWeb:
            // Root navigation data source is inherited
            break;
        case StandardNavigationSource.Unknown:
            // The documentation for this value states:
            // "Returns a value of unknown to indicate an advanced configuration
            // that does not correspond to one of the standard configurations.
            // This value cannot be manually assigned to the Source property."
            break;
    }
}
else
{
    // Non-publishing site, root source is SPNavigation.TopNavigationBar
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top