Programmatically Configure Navigation Hierarchies using Single value Choice field in SharePoint 2010

Updated: May 7, 2019

In this article we will be seeing how to configure Navigation Hierarchies using Single value choice field in SharePoint 2010 using C# and powershell script.

Refer this article to Configure Navigation Hierarchies using Single value Choice field in SharePoint 2010 through UI and basics of Navigation hierarchies.

Here we will be seeing the same thing programmatically and using powershell scripts.

Steps Involved:

a. Open Visual Studio 2010.

b. Create Console application.

c. Replace the code with the following.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement.MetadataNavigation;

  7. namespace NavigationHierarchy

  8. {

  9. class Program   

  10. {

  11. static void Main(string[] args)       

  12. {

  13. using (SPSite site = new SPSite("http://serverName:1111/"))           

  14. {

  15. using (SPWeb web = site.RootWeb)               

  16. {

  17. SPList list=web.Lists.TryGetList("cl");

  18. SPField field=list.Fields["Country"];

  19. MetadataNavigationSettings listNavSettings =MetadataNavigationSettings.GetMetadataNavigationSettings(list);

  20. MetadataNavigationHierarchy mdnNavHierarchy = newMetadataNavigationHierarchy(field;                                       

  21. listNavSettings.AddConfiguredHierarchy(mdnNavHierarchy);                 

  22. MetadataNavigationSettings.SetMetadataNavigationSettings(list, listNavSettings, true);                 

  23. }           

  24. }        

  25. }   

  26. }

  27. }

d. Hit F5.


Go to the List => List Settings =>General Settings =>Metadata navigation settings => Configure navigation hierarchies.
















You could see the field "Country" is added successfully to the Hierarchy Fields.