Programmatically Configure Key Filters in SharePoint 2010

In this article we will be seeing how to configure key filters in SharePoint 2010 using C#.

Key Filters appear just below the navigation hierarchy. Refer this Article to configure navigation hierarchy. Key Filters operate similar to the navigation hierarchy. They are used to filter the view of lists / document libraries.

The following fields are available for key filter fields are

  1. Content Type

  2. Choice Field

  3. Managed Metadata Field

  4. Person or Group Field

  5. Date and time field

  6. Number Field

I have a list and I am having the following items.


I am going to add the fields to the Key Filter Field using #

Steps Involved:

  • Open Visual Studio 2010.

  • Create Console application.

  • 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 ConfigureKeyFilter

  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. MetadataNavigationKeyFilter mdnKeyFilter=new MetadataNavigationKeyFilter(field);                   

  21. listNavSettings.AddConfiguredKeyFilter(mdnKeyFilter);                 

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

  23. }           

  24. }       

  25. }   

  26. }

  27. }

  • Hit F5.


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
















I have added "Country" field to the key filter field as shown in the following.


Click on Ok.

You could see the Key Filters in the left hand pane of the user interface.










Select some value from the key field and click on "Apply". You could see the view has been changed for the list based on the key field value.


Recent Posts

See All