Export (0) Print
Expand All

NavigationTermSet class

SharePoint 2013

Specifies navigation behavior and properties for a TermSet object that is used to drive the navigation and friendly URLs for a website.

System.Object
  Microsoft.SharePoint.Publishing.Navigation.NavigationTermSetItem
    Microsoft.SharePoint.Publishing.Navigation.NavigationTermSet

Namespace:  Microsoft.SharePoint.Publishing.Navigation
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)

[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public abstract class NavigationTermSet : NavigationTermSetItem

This class adds additional properties and operations that are specific to managed navigation.

The NavigationTermSet object has two modes: an editable mode that acts as a lightweight wrapper for a TermSet object, and an optimized read-only mode that is used to reduce the memory size of the NavigationTermSet objects stored in the taxonomy navigation cache.

Use the GetAsResolvedByWeb method to construct an editable instance. Use the TaxonomyNavigation methods to obtain read-only instances from the taxonomy navigation cache.

NoteNote

The NavigationTermSet class supports multiple views via the GetWithNewView method.

See NavigationTermSetView documentation for more information.

The following example demonstrates creating a term set.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Publishing.Navigation;


namespace NavigationDemos
{
    public class Class1
    {
        public static readonly Guid NavTermSetId = new Guid("<GUID>");
        public static readonly Guid TaggingTermSetId = new Guid("<GUID>");
        public const string ServerUrl = "http:// <ServerURL>";



        public void CreateNavigationTermSet()
        {
            using (SPSite site = new SPSite(ServerUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    TaxonomySession taxonomySession = new TaxonomySession(site, updateCache: true);

                    // Use the first TermStore in the list
                    if (taxonomySession.TermStores.Count == 0)
                        throw new InvalidOperationException("The Taxonomy Service is offline or missing");

                    TermStore termStore = taxonomySession.TermStores[0];

                    // Does the TermSet already exist?
                    TermSet existingTermSet = termStore.GetTermSet(NavTermSetId);
                    if (existingTermSet != null)
                    {
                        existingTermSet.Delete();
                        termStore.CommitAll();
                    }

                    // Create a new TermSet
                    Group siteCollectionGroup = termStore.GetSiteCollectionGroup(site);
                    TermSet termSet = siteCollectionGroup.CreateTermSet("Navigation Demo", NavTermSetId);

                    NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(termSet, web,
                        StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);

                    navTermSet.IsNavigationTermSet = true;
                    navTermSet.TargetUrlForChildTerms.Value = "~site/Pages/Topics/Topic.aspx";

                    NavigationTerm term1 = navTermSet.CreateTerm("Term 1", NavigationLinkType.SimpleLink);
                    term1.SimpleLinkUrl = "http://www.bing.com/";

                    Guid term2Guid = new Guid("87FAA433-4E3E-4500-AA5B-E04330B12ACD");
                    NavigationTerm term2 = navTermSet.CreateTerm("Term 2", NavigationLinkType.FriendlyUrl,
                        term2Guid);

                    // Verify that the NavigationTermSetView is being applied correctly

                    string expectedTargetUrl = web.ServerRelativeUrl
                        + "/Pages/Topics/Topic.aspx?TermStoreId=" + termStore.Id.ToString()
                        + "&TermSetId=" + NavTermSetId.ToString()
                        + "&TermId=" + term2Guid.ToString();

                    NavigationTerm childTerm = term2.CreateTerm("Term 2 child", NavigationLinkType.FriendlyUrl);

                    // Commit the Taxonomy changes
                    childTerm.GetTaxonomyTerm().TermStore.CommitAll();
                }
            }
        }
    }
}

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