Kategoriarkiv: WebNavigationSettings

Programmatically set navigation settings in SharePoint 2013

When working on a project where I needed to set the navigation for newly created sites programmatically, I had a hard time finding the correct properties and methods in the SharePoint object model to change all the navigation settings. After some researching I found that you need no less than 3 different objects representing the navigation of a website to be able to modify all the settings. These examples apply to SharePoint sites with publishing features enabled (navigation options differ on non-publishing sites), and just using the PublishingWeb.Navigation and WebNavigationSettings objects were sufficient for me. Other properties can be found in the SPWeb.Navigation object however.

These are the objects containing navigation properties:

  • web.Navigation (Namespace: Microsoft.SharePoint)
  • publishingWeb.Navigation (Namespace: Microsoft.SharePoint.Publishing)
  • WebNavigationSettings (Namespace: Microsoft.SharePoint.Publishing.Navigation)

Here are examples of how you instantiate these objects:

Once instantiated, you will be able to modify the navigation using these objects. I will show some examples of how to set different options on the Navigation settings page on a publishing site through code.

Select type of navigation

GlobalAndCurrent

There are two navigations. The Global navigation (also called Top navigation) and the Current navigation (also called left navigation or quicklaunch). Both of these can be set to different to use different sources. They can inherit from their parent (if a subsite only), use managed navigation, or structural navigation. All you need to do is select the webNavigationSettings object, choose which navigation to set, and select a source for that navigation.

TaxonomyProvider is Managed Metadata Navigation, PortalProvider is structural navigation, InheritFromParentWeb is self-explanatory, and Unknown leaves no radiobutton selected.

Show subsites and pages

To change these options you need to use the PublishingWeb object.

ShowSubsitesAndPages2

Managed navigation: Page settings

To change the values of these two checkboxes, use the WebNavigationSettings object again.

DefaultPageSettings

Managed navigation: Term set

To connect to a term set in code you can do something like this.

MetadataNavigation

Structural navigation: Sorting

Use the PublishingWeb.Navigation to set the ordering of the Structural navigation.

Sorting

Update
Finally, don’t forget to update your objects when you are done with them.

These examples are not a complete overview of what you can do with the navigation in SP2013, but the objects I have shown contain most of the properties needed to set the navigation. You just have to look at the object model in Visual Studio to find other options, and most are pretty self-explanatory.