Thursday, 3 March 2011

Configuring metadata navigation settings on SharePoint lists with PowerShell

A nice feature in SharePoint Server 2010 is the ability to provide list and document library navigation/filtering through metadata and content type association. In a nutshell, this provides the ability to switch on features in the SharePoint user interface that allow users to find information by filtering and navigating using metadata tags and content types as opposed to folders, which as we all know have their limitations. Further details on the feature can be found here.

The feature is configured in the user interface by selecting “Metadata navigation settings” from the list settings. This will display an administration interface similar to the one shown below:

image

As you can see from the screenshot above, I have two custom columns called Document Status (single-value Choice type) and Technology (single-value Managed Metadata type) available in the Available Hierarchy Fields and Available Key Filter Fields boxes ready to add as a navigation hierarchy or key filter. Note the default setting for Selected Hierarchy Fields is Folders.

For this example, I will use PowerShell to configure both the navigation hierarchy and key filters settings to include my custom columns. I will also show you how to add the Content Type and Folders options too.

First, we will assign the site that contains the list to a variable and use this variable to get the list:

#Get Web and List objects
$web = Get-SPWeb
http://portal/permuniquesite
$list = $web.Lists["Shared Documents"]

Next, we will get the current metadata navigation settings for the list and assign them to a variable:

#Get metadata navigation settings for the list
$listNavSettings = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::GetMetadataNavigationSettings($list)

The next two lines of the script clear the configured hierarchies and configured key filters on the list. You could use these commands in combination with the SetMetadataNavigationSettings and $list.RootFolder.Update() commands mentioned later to remove any currently configured metadata navigation settings for the list:

#Clear current metadata navigation settings on the list
$listNavSettings.ClearConfiguredHierarchies()
$listNavSettings.ClearConfiguredKeyFilters()

These commands will add my two custom columns to the Selected Key Filter Fields:

#Configure key filters by adding columns
$listNavSettings.AddConfiguredKeyFilter($list.Fields["Technology"])
$listNavSettings.AddConfiguredKeyFilter($list.Fields["Document Status"])

These next two lines are quite important. I have found that the navigation hierarchy configuration does not work when configuring it from PowerShell unless you add the default Folders item to the Selected Hierarchy Fields first. Without this step, the custom columns will be added successfully in the Metadata navigation settings page, but they will not appear in the Tree View interface:

#Add folder navigation hierarchi$listes into list settings
#This is required to enable and show navigation hierarchies in the Tree View
$folderHierarchy = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationHierarchy]::CreateFolderHierarchy()
$listnavSettings.AddConfiguredHierarchy($folderHierarchy)

The next step is optional, but contains the commands needed to add Content Type navigation hierarchies to the settings, if required:

#Optionally add content type navigation hierarchies into list settings if required
$ctHierarchy = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationHierarchy]::CreateContentTypeHierarchy()
$listnavSettings.AddConfiguredHierarchy($ctHierarchy)

We now add the custom columns to the Selected Hierarchy Fields:

#Configure extra navigation hierarchies by adding list columns
$listNavSettings.AddConfiguredHierarchy($list.Fields["Technology"])
$listNavSettings.AddConfiguredHierarchy($list.Fields["Document Status"])

And finally, set the metadata navigation settings and update the root folder of the list, which is where the settings are stored:

#Set the new metadata navigation settings and update the root folder of the list
[Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::SetMetadataNavigationSettings($list, $listNavSettings, $true)
$list.RootFolder.Update()

The Metadata navigation settings page should now look as follows:

image

To test the metadata navigation feature, you will need to switch on the Tree View setting for the site. This can be done in the UI by selecting Site Settings > Tree view > Enable Tree View or you can change it with a couple of extra lines in your PowerShell script:

#Enable Tree View on the site so that navigation hierarchies can be used in the UI
$web.TreeViewEnabled = $true
$web.Update()

image

The metadata navigation and key filters features will show up on the left-hand side of the SharePoint user interface, underneath the quick launch menu, as shown above. To complete the script, don’t forget to dispose of the Web object:

#Dispose of the Web object
$web.Dispose()

14 comments:

  1. Hey Phil,

    This worked great for me, but a question. How can we add the "All Tags" column? I believe this represents the enterprise keywords field. This is treated like a special type like content type, but there's no special method for adding it. I tried all tags and enterprise keywords, and it didnt work. Ideas? The MetadataNavigationKeyFilter class says its there:

    "If it is the special Enterprise Keywords field, then the filter will be an All Tags filter, which enables you to find items with the managed metadata term in any of the Managed Metadata fields in the item".

    Thanks as always!

    ReplyDelete
  2. I dont think you need to update root folder.

    ReplyDelete
  3. I can not get this to work properly. Using the code above, I am able to add all the correct settings to the list, however the navigation controls only flash for a fraction of a second when the page loads, and then they disapear again.

    If I then go into the metadata navigation settings for the list, all the correct settings are there, and when I press "ok" and go back to the list, everything is working as intended. How can I add the "OK" part into the script?

    ReplyDelete
    Replies
    1. I figured out what was causing this behaviour. I was clearing the configured hierarchies and key filters and not updating it before adding the new fields. What I had to do instead was to first clear previous filters, then update the navigation settings, and then fetch a new $listNavSettings object, add the new filters, and call update again:
      first:
      $listNavSettings = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::GetMetadataNavigationSettings($list)
      $listNavSettings.ClearConfiguredHierarchies()
      $listNavSettings.ClearConfiguredKeyFilters()
      [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::SetMetadataNavigationSettings($list, $listNavSettings, $true)
      $list.RootFolder.Update()

      $listNavSettings = [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::GetMetadataNavigationSettings($list)
      $listNavSettings.AddConfiguredHierarchy($myField)
      $listNavSettings.AddConfiguredKeyFilter($myField)

      [Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings]::SetMetadataNavigationSettings($list, $listNavSettings, $true)
      $list.RootFolder.Update()

      Delete
    2. My previous post wasnt entierly true, after further investigation. In my case I had a different problem, where the key filters and hierarchies would flash for a fraction of a second and then dissapear. All the settings were correct, and even if I did the settings manually through the the sharepoint GUI, it still didnt work.

      So the problem was in the custom list view that the document library was using. Im not sure how or why, but inserting some markup fixed my problem, using this blog: http://tihomirignatov.blogspot.no/2010/08/how-to-use-metadata-navigation-in.html

      I noticed something interesting when investigating this too. The URL to the view (http://server/web/list/Forms/dokumenter.aspx?woid=1&NavIndex=1 &HideFolderView=true&RootFolder=/site/web/list/1/Dokumenter&Source=1) contains an argument "HideFolderView=true". If I manually changed said argument to "false" instead, then the key filters and hierarchies would show as expected. Just saying.

      Delete