Changing a web site’s navigation settings is pretty easy using the SharePoint user interface. You just click the Site Actions button, select the Site Settings option, and click on Navigation. But how do you do it programmatically?

Most navigation options on a SharePoint web site are manipulated through the SPWeb object’s property bag. Set the following properties to configure a web site’s navigation settings:

  • __IncludeSubSitesInNavigation: Set this property to True to show the subsites of the current site on the global and current navigation menus. Set it to False to hide the subsites.
  • __IncludePagesInNavigation: Set this property to True to show the pages on the global and current navigation menus. Set it to False to hide the pages.
  • __InheritCurrentNavigation: Set this property to True to display the same navigations items as the parent site in the current navigation menu. Set it to False to display only items from the current site and subsites.
  • __NavigationShowSiblings: Set this property to True to display the navigation items below the current site and the current site’s siblings, in the current navigation menu.
  • __NavigationOrderingMethod: Set this property to one of the following values:
    • 0: Sort the navigation items automatically
    • 1: Sort the subsites manually but the pages automatically
    • 2: Sort the navigation items manually
  • __NavigationAutomaticSortingMethod: When using automatic ordering, set this property to one of the following values:
    • 0: Sort by Title
    • 1: Sort by Created Date
    • 2: Sort by Last Modified Date
  • __NavigationSortAscending: When using automatic ordering, set this property to True if the automatic sorting of the navigation items should be done in ascending order. Set it to False to use descending order.

Note: there are two underscore characters (_) in each property name.

Example
In the example below, I’m setting the navigation options to:

  • Show only the current site’s navigation items in the global navigation
  • Show the current site’s sub sites in the global navigation
  • Don’t show the pages in the global navigation
  • Show only the current site’s navigation items in the current navigation
  • Don’t show the current site’s siblings in the current navigation
  • Sort the navigation items manually
// break inheritance of global navigation
web.Navigation.UseShared = false;

// show subwebs but don't show pages in global navigation
web.AllProperties["__IncludeSubSitesInNavigation"] = "True";
web.AllProperties["__IncludePagesInNavigation"] = "False";

// show only current site's navigation items in current navigation
web.AllProperties["__InheritCurrentNavigation"] = "False";
web.AllProperties["__NavigationOrderingMethod"] = "2";
web.AllProperties["__NavigationShowSiblings"] = "False";
web.Update();

LEAVE A REPLY

Please enter your comment!
Please enter your name here