Programmatically Configuring Metadata Navigation

Metadata navigation offers a quite clever way to navigate documents within a library. It cuts across folders, and allows drill-in for taxonomies as configured hierarchies, and also allows for a combination of fields for key filters. One can configure it manually for a library, but how can one do this programmatically? Below are the steps:

# get the SPWeb:
$web = Get-SPWeb "http://WhateverTheSPWebSiteIs"
# get the library:
$JPLib = $web.Lists.TryGetList("WhateverTheListIsCalled")
# Here's the XML object we are coding against:
$listNavSettings = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::GetMetadataNavigationSettings($JPLib)
# You can output the XML settings and easily see its configuration each step along the way with this:
# Here's how to clear both Configured Hierarchies, and Key Filters:
[Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::SetMetadataNavigationSettings($JPLib, $listNavSettings, $true)
# Let's get ready for a Content Type Hierarchy
$ctHierarchy = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationHierarchy]::CreateContentTypeHierarchy()
# Add a configured Hierarchy:
$listNavSettings.AddConfiguredHierarchy($JPLib.Fields["Field Name"])
# Add a Content Type Key Filter; I chose this on purpose, as using "Content Type" will not work, the field to use here is "Content Type ID":
$listNavSettings.AddConfiguredKeyFilter($JPLib.Fields["Content Type ID"])
# Now the party ends happily with an update; note no $list.update() or $web.update() is needed:
[Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::SetMetadataNavigationSettings($JPLib, $listNavSettings, $true)
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *