Partilhar via


MetadataNavigationSettings Class

Configures which settings on a MetadataNavigationItem object on an SPList object control the display of metadata hierarchies and filters on list views.

Inheritance Hierarchy

System.Object
  Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings

Namespace:  Microsoft.Office.DocumentManagement.MetadataNavigation
Assembly:  Microsoft.Office.DocumentManagement (in Microsoft.Office.DocumentManagement.dll)

Syntax

'Declaration
Public Class MetadataNavigationSettings
'Usage
Dim instance As MetadataNavigationSettings
public class MetadataNavigationSettings

Remarks

Gets settings from and sets settings to an SPList object and persists them such that the list, site hierarchy tree, and filter controls can use them in the list view page UI. Always use the [GetMetadataNavigationSettings] static method to get settings from an SPList object when initializing new instances of the MetadataNavigationSettings object.

By default, all lists have settings where no key filters are configured and only the special folder hierarchy type is configured, which shows only the SPFolder object in the tree. Use these methods to modify metadata hierarchy and key filter data in the tree.

Method

Action

[AddConfiguredHierarchy] method to add additional metadata hierarchies and key filters to settings.

Adds metadata hierarchies to settings.

[AddConfigureKeyFilter]

Adds key fliters to settings.

[FindConfiguredHierarchy]

Determines whether configured hierarchies exist in settings.

Use the result on the original settings instead of creating a new hierarchy to ensure that the internal view settings are preserved. After adjusting the MetadataNavigationSettings object originally retrieved from the list, set the adjusted settings back on the list with the SetMetadataNavigationSettings() object.

[FindConfiguredKeyFilter]

Determines whether configured items exist in the settings.

[ClearConfiguredHierarchies]

Empties settings and then adds new hierarchies in the desired order.

[ClearConfiguredKeyFilters]

Empties settings and then adds key filters in the desired order.

Examples

using SPWeb = Microsoft.SharePoint.SPWeb;
using SPList = Microsoft.SharePoint.SPList;
using SPField = Microsoft.SharePoint.SPField;

using MetadataNavigationSettings = Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationSettings;
using MetadataNavigationHierarchy = Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationHierarchy;
using MetadataNavigationKeyFilter = Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationKeyFilter;
using MetadataNavigationContext = Microsoft.Office.DocumentManagement.MetadataNavigation.MetadataNavigationContext;

namespace Microsoft.SDK.SharePointServer.Samples
{
    public static class MetadataNavigationSettingsSamples
    {
        public static void AddFieldsToMDNSettings(SPList list)
        {
            // TODO: Replace these variable values and input parameters with your own values.
            //
            // internal name of the SPField to add into the key filters
            string fieldNameKeyFilterToAdd = "Modified";

            // Validate the input parameters.
            //
            if (null == list)
            {
                throw new System.ArgumentNullException("list");
            }

            // Get the MDN settings object for the SPList that was passed in.
            //
            MetadataNavigationSettings mdnSettings =
                MetadataNavigationSettings.GetMetadataNavigationSettings(list);

            // Add hiearchies into the settings if they are not already there
            //
            MetadataNavigationHierarchy folderHierarchyToAdd =
                MetadataNavigationHierarchy.CreateFolderHierarchy();
            if (null == mdnSettings.FindConfiguredHierarchy(folderHierarchyToAdd.FieldId))
            {
                mdnSettings.AddConfiguredHierarchy(folderHierarchyToAdd);
            }

            MetadataNavigationHierarchy contentTypeHierarchyToAdd =
                MetadataNavigationHierarchy.CreateContentTypeHierarchy();
            if (null == mdnSettings.FindConfiguredHierarchy(contentTypeHierarchyToAdd.FieldId))
            {
                mdnSettings.AddConfiguredHierarchy(contentTypeHierarchyToAdd);
            }

            // Add key filters into the settings if they are not already there
            //
            SPField newKeyFilterField = list.Fields[fieldNameKeyFilterToAdd];
            if (null == mdnSettings.FindConfiguredKeyFilter(newKeyFilterField.Id))
            {
                mdnSettings.AddConfiguredKeyFilter(new MetadataNavigationKeyFilter(newKeyFilterField));
            }

            MetadataNavigationKeyFilter contentTypeKeyFilter =
                MetadataNavigationKeyFilter.CreateContentTypeKeyFilter();
            if (null == mdnSettings.FindConfiguredKeyFilter(contentTypeKeyFilter.FieldId))
            {
                mdnSettings.AddConfiguredKeyFilter(contentTypeKeyFilter);
            }

            // Set the MDN settings back into the list and automatically adjust indexing.
            //
            MetadataNavigationSettings.SetMetadataNavigationSettings(list, mdnSettings, true);
        }
    }
}

namespace Microsoft.SDK.SharePointServer.Samples.WebControls
{

    public static class MetadataNavigationSettingsWebSamples
    {
        public static void ToggleMDNSettingsStateInWebRequest(SPWeb web, System.Guid listId)
        {
            // Validate the input parameters.
            //
            if (null == web)
            {
                throw new System.ArgumentNullException("web");
            }

            // Ensure that there is a current context for these operations
            //
            if (null == MetadataNavigationContext.Current)
            {
                throw new System.InvalidOperationException("No current web context or MetadataNavigationContext");
            }

            // Get the MDN settings for the input web and list id using the cache
            //
            System.Guid listIdToUse = listId;
            MetadataNavigationSettings mdnSettings =
                MetadataNavigationSettings.GetMetadataNavigationSettings(web, listId);

            // If no settings were retrieved then try the current context settings.
            //
            if (null == mdnSettings)
            {
                mdnSettings = MetadataNavigationSettings.Current;
            }

            if (null == mdnSettings)
            {
                throw new System.ArgumentException("Cannot retrieve setting from inputs or from current context");
            }

            if(System.Guid.Empty != MetadataNavigationContext.Current.ListId)
            {
                listIdToUse = MetadataNavigationContext.Current.ListId;
            }

            if (mdnSettings.IsEnabled)
            {
                // If the current settings are enabled then clear them back to default (which is only folder hierarchy)
                mdnSettings.ClearConfiguredHierarchies();
                mdnSettings.ClearConfiguredKeyFilters();
                mdnSettings.AddConfiguredHierarchy(MetadataNavigationHierarchy.CreateFolderHierarchy());
            }
            else
            {
                // If they are not enabled then add a content type key filter and hierachy
                mdnSettings.AddConfiguredHierarchy(MetadataNavigationHierarchy.CreateContentTypeHierarchy());
                mdnSettings.AddConfiguredKeyFilter(MetadataNavigationKeyFilter.CreateContentTypeKeyFilter());
            }

            // Set the MDN settings back into the list.
            //
            SPList list = web.Lists[listIdToUse];
            MetadataNavigationSettings.SetMetadataNavigationSettings(list, mdnSettings);
        }

    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

MetadataNavigationSettings Members

Microsoft.Office.DocumentManagement.MetadataNavigation Namespace

GetMetadataNavigationSettings(SPWeb, Guid)

GetMetadataNavigationSettings(SPWeb, Guid, Boolean)

SetMetadataNavigationSettings(SPList, MetadataNavigationSettings)

SetMetadataNavigationSettings(SPList, MetadataNavigationSettings, Boolean)

SetMetadataNavigationSettings(SPList, MetadataNavigationSettings, Boolean, Boolean)

MetadataNavigationHierarchy

MetadataNavigationKeyFilter