How to: Deploy Metadata Navigation without using code
Posted
Wednesday, February 9, 2011 4:37 PM
by
CoreyRoth
As someone who focuses mostly on ECM and Search, I am a huge fan of the new Metadata Navigation feature on document libraries in SharePoint 2010. It provides a great alternative to folders when it comes to navigating your document libraries. A list administrator can configure Metadata navigation on the document library settings page. However, I prefer to make changes using CAML or code so that I can deploy them easily to other environments. Usually, my first choice is with CAML, so I did some digging today and I discovered the trick to deploying your Metadata navigation settings using a SharePoint feature.
When I first started investigating this, I assumed it might be some new element or attribute inside the schema.xml file of the list. It turns out my assumption was incorrect. We actually set this by assigning some XML to the client_MOSS_MetadataNavigationSettings property on the root folder of the list. We can assign this value using code, but you know I prefer to use CAML. We can use the PropertyBag element to make this happen. Before we look at the PropertyBag element itself though, let’s look at the underlying XML. Let’s take my list here with three items selected for Metadata Navigation: a site column named DocumentType, the Content Type of the documents in the library, and the Folders in the library itself. Here is what the XML will look like.
<MetadataNavigationSettings SchemaVersion="1" IsEnabled="True" AutoIndex="True">
<NavigationHierarchies>
<FolderHierarchy HideFoldersNode="False" />
<MetadataField FieldID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" FieldType="Choice" CachedName="DocumentType" CachedDisplayName="DocumentType" />
<MetadataField FieldID="03e45e84-1992-4d42-9116-26f756012634" FieldType="ContentTypeId" CachedName="ContentTypeId" CachedDisplayName="Content Type" />
</NavigationHierarchies>
<ManagedIndices>
<ManagedIndex IndexID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" IndexFieldName="DocumentType" IndexFieldID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" />
</ManagedIndices>
</MetadataNavigationSettings>
The first line seems to always be the same. It enables the Metadata Navigation and it will take care of automatically adding new index columns when AutoIndex is set to true. The NavigationHierarchies section actually defines which fields will be used for Metadata navigation. If you want to navigate by folders, add a FolderHierarchy element and set HideFoldersNode to false. If you set it to true, folder navigation will not be present. The MetadataField element defines which fields to use for navigation. You need the GUID for each site column to include. You can get this from your list’s schema.xml file. You then need to specify the FieldType. Remember that it only supports Single-value Choice (Choice), Managed Metadata (TaxonomyFieldType) , and Content Type (ContentTypeId) field types. You then need to specify the field name in the CachedName field and then you can customize the display name as you see fit with CachedDisplayName. The last thing to note here is that you have to create a Managed Index for any choice fields you add. This is why you see a ManagedIndex element with the DocumentType field. Set the IndexId and IndexFieldId attributes equal to the Id of the field.
At this point, we are ready to assign this XML to the the client_MOSS_MetadataNavigationSettings property. We need to encode the above XML, because it is being stored inside an attribute of another XML document. Here is what your elements.xml file would look like.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<PropertyBag Url="Shared Documents" ParentType="Folder" RootWebOnly="FALSE" xmlns="http://schemas.microsoft.com/sharepoint/">
<Property Name="client_MOSS_MetadataNavigationSettings" Value="<MetadataNavigationSettings SchemaVersion="1" IsEnabled="True" AutoIndex="True"><NavigationHierarchies><FolderHierarchy HideFoldersNode="True" /><MetadataField FieldID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" FieldType="Choice" CachedName="DocumentType" CachedDisplayName="DocumentType" /> <MetadataField FieldID="03e45e84-1992-4d42-9116-26f756012634" FieldType="ContentTypeId" CachedName="ContentTypeId" CachedDisplayName="Content Type" /> </NavigationHierarchies><ManagedIndices><ManagedIndex IndexID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" IndexFieldName="DocumentType" IndexFieldID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" /><ManagedIndex IndexID="d31655d1-1d5b-4511-95a1-7a09e9b75bf2" IndexFieldName="Editor" IndexFieldID="d31655d1-1d5b-4511-95a1-7a09e9b75bf2" /></ManagedIndices></MetadataNavigationSettings>" Type="string" />
</PropertyBag>
</Elements>
It’s kind of hard to read the contents of the Property element, but it is just the encoded version of the XML above. As for the PropertyBag element, you just specify the relative Url to the document library on your site. In this case, it’s Shared Documents. Always set ParentType to Folder and RootWebOnly to false. At this point, you can activate this feature after you have deployed your document library and it will enable the Metadata navigation. Here is what the Metadata Navigation settings page looks like.
Here is what my document library looks like with the navigation enabled.
It’s pretty easy to set this up as you can see. Keep in mind that if you set this value, it will overwrite any values previously stored. This includes any Key Filters or Indexes that might already be present on the list.
Speaking of Key Filters, we can add them to the document library using the client_MOSS_MetadataNavigationSettings as well. Let’s look at some more XML. In this case, I am adding key filters for DocumentType, All Tags, and Modified By (editor).
<MetadataNavigationSettings SchemaVersion="1" IsEnabled="True" AutoIndex="True">
<KeyFilters>
<MetadataField FieldID="3c6f8f63-0616-437c-80eb-cf7cba0d88cc" FieldType="Choice" CachedName="DocumentType" CachedDisplayName="DocumentType" />
<MetadataField FieldID="23f27201-bee3-471e-b2e7-b64fd8b7ca38" FieldType="TaxonomyFieldTypeMulti" CachedName="TaxKeyword" CachedDisplayName="All Tags" />
<MetadataField FieldID="d31655d1-1d5b-4511-95a1-7a09e9b75bf2" FieldType="User" CachedName="Editor" CachedDisplayName="Modified By" />
</KeyFilters>
<ManagedIndices>
<ManagedIndex IndexID="d31655d1-1d5b-4511-95a1-7a09e9b75bf2" IndexFieldName="Editor" IndexFieldID="d31655d1-1d5b-4511-95a1-7a09e9b75bf2" />
</ManagedIndices>
</MetadataNavigationSettings>
The elements are pretty similar to that of the NavigationHierarchies element. You add one MetadataField element for each field you want with the same attributes as before. However, a few more fields types are supported such as Person or Group (User). It also recognizes Enterprise Keywords through the use of the All Tags (TaxonomyFieldMulti) filter. You can also do Date and Time fields as well as Number fields too. I also added the ManagedIndex field for Editor (the Modified By filter). You can probably leave the indexes out since AutoIndex is true, but if you run into issues, you can add them manually as you see above. You can deploy KeyFilters and NavigationHierarchies elements at the same time. You’ll need to encode the XML again just as you did before. When you activate the feature, you’ll have Key Filters enabled on your document library.
Using the PropertyBag element, you can easily add metadata navigation to your document libraries. This is a great alternative to defining these settings using code.