How to: Use code to pin terms to navigation term sets in SharePoint 2013

SharePoint 2013

Learn how to use code to pin terms to navigation term sets.

Last modified: November 08, 2013

Applies to: SharePoint Server 2013

In taxonomy creation, pinning is the ability to attach a term to a target. SharePoint 2013 introduces term pinning. A pinned term is just like a term that is reused, except it is read-only and cannot be changed in the location where the term is used.

In SharePoint 2013 managed navigation, the API enables you to pin new or existing terms to a NavigationTermSet object. In Microsoft SharePoint Server 2010, users could reuse terms (and all terms nested under the reused terms) in other locations in the term hierarchy. After these terms were reused, they could be modified in any location and changes would be seen everywhere the terms were reused.

To understand pinning in SharePoint 2013, you may want to learn about managed metadata, terms, term sets, managed navigation, the term store, and other related concepts and APIs. Table 1 describes articles that give more information about pinning.

Table 1. Core concepts for pinning

Article title

Description

A Brief Introduction to Enterprise Metadata Management for Microsoft SharePoint Server 2010 Developers

Written for SharePoint Server 2010, this article provides a basic overview of the enterprise managed metadata programming model and core concepts, such as terms and term sets.

Managed navigation in SharePoint 2013

An introduction to the taxonomy-driven managed navigation feature in SharePoint 2013.

You can use custom code from the .NET server, .NET client (CSOM), Silverlight, or JavaScript programming models to complete pinning operations on terms and term sets. The following .NET server code examples include a test for pinning terms to navigation term sets, and a method that you can use to test whether a Term object is pinned to a specified TermSet object. Then, the test creates Term objects, and pins one of them to the specified NavigationTermSet object.

To pin terms to navigation term sets

  • The following sample tests pinning terms to navigation term sets. It uses the NavigationTermSet object, which contains methods and properties that are handy in managed navigation scenarios, such as creating taxonomy-driven site navigation menus.

    The sample first checks whether a NavigationTermSet object exists. If one doesn't exist, then the code creates a NavigationTermSet. (If one already exists, the code deletes the old one before it creates a new one). Then, after the code creates some Term objects to pick from, it creates a publishing page (.aspx) file for demonstration purposes, sets it as the custom target page for pinned terms, and then pins some navigation properties to the page.

    public void TermPinningTest()
            {
    using (SPSite site = new SPSite(TestConfig.ServerUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        TaxonomySession taxonomySession = new TaxonomySession(site, updateCache: true);
    
                        // Create the navigation term set.
                        NavigationTermSet menuNavTermSet = DemoUtilities.SetUpSampleNavTermSet(
                            this.TestContext, taxonomySession, web);
                        TermSet menuTaxTermSet = menuNavTermSet.GetTaxonomyTermSet();
    
                        TermStore termStore = menuTaxTermSet.TermStore;
                        Group group = menuTaxTermSet.Group;
    
                        // Does the tagging Taxonomy term set already exist?
                        TermSet taggingTaxTermSet = group.TermSets.FirstOrDefault(
                            ts => ts.Id == TestConfig.TaggingTermSetId);
    
                        if (taggingTaxTermSet != null)
                        {
                            Log("Deleting old tagging term set");
    
                            // If the tagging Taxonomy term set already exists, delete the old one.
                            taggingTaxTermSet.Delete();
                            termStore.CommitAll();
                        }
    
                        Log("Creating the tagging term set");
    
                        taggingTaxTermSet = group.CreateTermSet("Demo Tagging TermSet", TestConfig.TaggingTermSetId);
    
                        int lcid = termStore.WorkingLanguage;
    
                        // Create some terms.
                        Term taggingProductsTaxTerm = taggingTaxTermSet.CreateTerm("Products", lcid);
                        taggingProductsTaxTerm.CreateTerm("Electronics", lcid);
                        taggingProductsTaxTerm.CreateTerm("Footwear", lcid);
                        taggingProductsTaxTerm.CreateTerm("Sports", lcid);
    
                        termStore.CommitAll();
    
                        /// Pinning the products subtree. Pins the "Products" Term object to the NavigationTermSet object.
                        Term menuProductsTaxTerm = menuTaxTermSet.ReuseTermWithPinning(taggingProductsTaxTerm);
                        termStore.CommitAll();
    
                        /// Creating the publishing page template DemoTargetPage.aspx");
                        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
    
                        SPListItem pageListItem = null;
                        PublishingPage publishingPage;
                        try
                        {
                            pageListItem = web.GetListItem(web.Url + "/Pages/DemoTargetPage.aspx");
                            publishingPage = PublishingPage.GetPublishingPage(pageListItem);
       
                        }
                        catch (FileNotFoundException)
                        {
                            Log("Creating new target page");
                            publishingPage = publishingWeb.AddPublishingPage("DemoTargetPage.aspx", publishingWeb.DefaultPageLayout);
                            Log("Checking in target page draft");
                            publishingPage.CheckIn("TermPinningTest");
                        }
    
                        // Set a custom target page for the pinned terms and then set some navigation properties.
    
                        // Normally the navigation objects are obtained by way of an optimized function such as
                        // TaxonomyNavigation.GetTermSetForWeb() or TaxonomyNavigationContext.Current.NavigationTerm.
                        // The function guarantees that URLs will be resolved using a valid NavigationTerm.View.
                        // But because we are populating totally new data, the cache will probably not be updated
                        // yet, so instead we manually construct a view using GetAsResolvedByWeb().
                        NavigationTerm menuProductsNavTerm = NavigationTerm.GetAsResolvedByWeb(menuProductsTaxTerm,
                            web, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);
    
                        menuProductsNavTerm.TargetUrl.Value = publishingPage.Uri.AbsolutePath;
                        menuProductsNavTerm.TargetUrlForChildTerms.Value = publishingPage.Uri.AbsolutePath;
    
                        termStore.CommitAll();
    
                        Log("TermPinningTest completed successfully");
                    }
                }
    
    }
    
Show:
© 2014 Microsoft