in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

How to: Deploy Metadata Navigation without using code

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="&lt;MetadataNavigationSettings SchemaVersion=&quot;1&quot; IsEnabled=&quot;True&quot; AutoIndex=&quot;True&quot;&gt;&lt;NavigationHierarchies&gt;&lt;FolderHierarchy HideFoldersNode=&quot;True&quot; /&gt;&lt;MetadataField FieldID=&quot;3c6f8f63-0616-437c-80eb-cf7cba0d88cc&quot; FieldType=&quot;Choice&quot; CachedName=&quot;DocumentType&quot; CachedDisplayName=&quot;DocumentType&quot; /&gt; &lt;MetadataField FieldID=&quot;03e45e84-1992-4d42-9116-26f756012634&quot; FieldType=&quot;ContentTypeId&quot; CachedName=&quot;ContentTypeId&quot; CachedDisplayName=&quot;Content Type&quot; /&gt; &lt;/NavigationHierarchies&gt;&lt;ManagedIndices&gt;&lt;ManagedIndex IndexID=&quot;3c6f8f63-0616-437c-80eb-cf7cba0d88cc&quot; IndexFieldName=&quot;DocumentType&quot; IndexFieldID=&quot;3c6f8f63-0616-437c-80eb-cf7cba0d88cc&quot; /&gt;&lt;ManagedIndex IndexID=&quot;d31655d1-1d5b-4511-95a1-7a09e9b75bf2&quot; IndexFieldName=&quot;Editor&quot; IndexFieldID=&quot;d31655d1-1d5b-4511-95a1-7a09e9b75bf2&quot; /&gt;&lt;/ManagedIndices&gt;&lt;/MetadataNavigationSettings&gt;" 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.

MetadataNavigationSettings

Here is what my document library looks like with the navigation enabled.

MetadataNavigationExpanded

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.

MetadataKeyFilters

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. 

Comments

 

Twitter Trackbacks for How to: Deploy Metadata Navigation without using code - Corey Roth [dotnetmafia.com] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 How to: Deploy Metadata Navigation without using code - Corey Roth         [dotnetmafia.com]        on Topsy.com

February 9, 2011 5:21 PM
 

Matt Bramer said:

Maybe it's just me, but this looks like code to me.  Where is the schema.xml located btw?

February 9, 2011 7:12 PM
 

CoreyRoth said:

@Matt.  Well one could argue XML is code but in this case I am comparing to using the API with C#.  The schema.xml is what you use to built a custom list using CAML.  If you are looking at an existing list, you can get the site column id by going to list settings, clicking on the column of interest and grab it from the URL.

February 10, 2011 1:29 PM
 

Deploy Metadata Navigation Using CAML « Sladescross's Blog said:

Pingback from  Deploy Metadata Navigation Using CAML &laquo; Sladescross&#039;s Blog

February 11, 2011 3:19 AM
 

Deploy Metadata Navigation Using CAML « Sladescross's Blog said:

Pingback from  Deploy Metadata Navigation Using CAML &laquo; Sladescross&#039;s Blog

February 11, 2011 3:19 AM
 

Rahul said:

"Deploy Metadata Navigation without using code". I thought how it is possible? But after seeing your blog only i came to know how simple it is.. Good job Mr.Corey. Thanks for your different blog post. keep posting different posts always. Simply by placing controls itself you deployed Metadate Navigation. This idea is simply great.

February 17, 2011 5:35 AM
 

shansd said:

Is this method available for Office 365? Thanks

July 19, 2011 4:34 AM
 

CoreyRoth said:

@shansd I think it should be.  You may have issue with the managed metadata fields, but the rest should work since it doesn't rely on code.

July 20, 2011 9:05 AM
 

SteveJeffery said:

Excellent! I was looking for a good use for property bags generated when importing site template (WSP).

Like you I was convinced there must be a way using CAML for my site definition - much more elegant than using code.

August 1, 2011 6:51 AM
 

Salman said:

Many thanks for your post. I was trying to set some key filters for record library. When I used this approach through a feature it gave me error that folder not found. here is the elements file.

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="schemas.microsoft.com/.../">

 <PropertyBag Url="Record Library" ParentType="Folder" RootWebOnly="FALSE">

   <Property Name="client_MOSS_MetadataNavigationSettings"

             Value="&lt;MetadataNavigationSettings SchemaVersion=&quot;1&quot; IsEnabled=&quot;True&quot; AutoIndex=&quot;True&quot;&gt;

             &lt;KeyFilters&gt;

             &lt;MetadataField FieldID=&quot;f9a44731-84eb-43a4-9973-cd2953ad8646&quot; FieldType=&quot;DateTime&quot; CachedName=&quot;_vti_ItemDeclaredRecord&quot; CachedDisplayName=&quot;Declared Record&quot; /&gt;

             &lt;MetadataField FieldID=&quot;1df5e554-ec7e-46a6-901d-d85a3881cb18&quot; FieldType=&quot;User&quot; CachedName=&quot;Author&quot; CachedDisplayName=&quot;Created By&quot; /&gt;

             &lt;MetadataField FieldID=&quot;c042a256-787d-4a6f-8a8a-cf6ab767f12d&quot; FieldType=&quot;Computed&quot; CachedName=&quot;ContentType&quot; CachedDisplayName=&quot;Content Type&quot; /&gt;

             &lt;/KeyFilters&gt;

             &lt;/MetadataNavigationSettings&gt;">      

   </Property>

 </PropertyBag>

</Elements>

and this is the logs error message, Can you please let me know what I have done wrong here.

"Adding properties to the folder with url Record Library failed while activating feature, the folder does not exist."

I would appreciate your help.

August 5, 2011 9:04 AM
 

Setup Metadata Navigation using CAML | Sean Carter said:

Pingback from  Setup Metadata Navigation using CAML | Sean Carter

February 24, 2013 12:46 AM
 

Labhesh said:

Hello Team,

I am trying to do the same using CSOM script, but unable to do so.

anyone did the same using CSOM please share the tips.

I know this can be done using ps script. get-spscripts.com/.../configuring-metadata-navigation.html

Tx

May 27, 2014 6:10 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems