2

Does anyone know how to add only one custom MetadataNavigationHierarchy? In my case, I would like to remove Folders from the TreeView, so my custom MetadataNavigationHierarchy is the only one being displayed.

I'm currently working on a Console application that's setting up Metadata navigation on an existing SharePoint 2013 document library containing existing folders and files.

Everything is working as it's supposed to, but I would like to remove Folders from the navigation.

Working code I use to add my custom value Tema:

MetadataNavigationSettings mdnSettings = MetadataNavigationSettings.GetMetadataNavigationSettings(list);    

if (taxonomyField != null)
{
    MetadataNavigationHierarchy hierarchyToAdd = new MetadataNavigationHierarchy(taxonomyField);

    if (null == mdnSettings.FindConfiguredHierarchy(taxonomyField.TermSetId))
    {
        mdnSettings.AddConfiguredHierarchy(hierarchyToAdd);
    }
}
else
{
    Helper.WriteError("Could not find Tema taxonomy field");
}

MetadataNavigationSettings.SetMetadataNavigationSettings(list, mdnSettings, true);
list.RootFolder.Update();

taxonomyField is the TermSet Tema I would like to display.

After the code runs, the "Selected Hierarchy Fields" are Tema and Folders. If I remove Folders manually in the GUI, everything workes like it's supposed to.

I've tried to use mdnSettings.ClearConfiguredHierarchies(); first, to remove Folders. This sets up the correct fields in "Library Settings --> Metadata Navigation Settings", but does not display the Metadata navigation when viewing the document library. If I click "Ok" on "Metadata Navigation Settings", Folders is added automatically, and both Tema and Folders is displayed in the document library. And again, if i remove Folders, then click "Ok" everything works as I want it to.

Visual representation of Folders, which i would like to remove programmatically. Do not want "Folders" here

Grevling
  • 456
  • 4
  • 18

1 Answers1

3

MetadataNavigationSettings contains FolderHierarchy hierarchy for that purpose, the attribute HideFoldersNode defines whether to remove or add Folder hierarchy field:

<FolderHierarchy HideFoldersNode=\"True\" />

But for some reason(!) MetadataNavigationSettings class does not expose that property, it is declared as an internal property.

How to add/remove Folder hierarchy field using SharePoint OM

The following method demonstrates how to bypass this limitation and allows to set HideFoldersNode property of MetadataNavigationSettings class:

public class MetadataNavigationSettingsHelper
{
    public static void SetHideFoldersNode(MetadataNavigationSettings settings,bool value)
    {
        var t = settings.GetType();
        t.InvokeMember("HideFoldersNode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, settings, new object[] { value });
    }
}

Example

The following example demonstrates how to add taxonomy field into navigation hierarchy and remove Folder field:

private static void AddTaxonomyFieldToHierarchy(SPList list, TaxonomyField taxonomyField)
{
     MetadataNavigationSettings mdnSettings = MetadataNavigationSettings.GetMetadataNavigationSettings(list);
     MetadataNavigationSettingsHelper.SetHideFoldersNode(mdnSettings,true);  //remove Folder

     MetadataNavigationHierarchy hierarchyToAdd = new MetadataNavigationHierarchy(taxonomyField);
     if (null == mdnSettings.FindConfiguredHierarchy(taxonomyField.Id))
     {
         mdnSettings.AddConfiguredHierarchy(hierarchyToAdd);
     }
     MetadataNavigationSettings.SetMetadataNavigationSettings(list, mdnSettings, true);
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193