How to Set Custom Sort Order For Taxonomy Terms in SharePoint 2013

In this article you will see how to set a custom sort order for the taxonomy terms in SharePoint 2013 using the server object model.

Introduction

I have created a taxonomy group in which I have a termset. In the termset I have created a few terms. I want to change the order of the child terms for a particular term using the SharePoint object model. In this article you will see how to do custom sort order for taxonomy terms using SharePoint object model. Open Central Administration. Click on "Application Management". Click on "Manage service applications" available under Service Applications. Click on "Managed Metadata Service Application".

taxonomy-terms-in-SharePoint 2013.jpg

Select the "Term2"; on the right hand side you will see the properties of the term. Click on the "CUSTOM SORT" tab; by default "Use default sort order according to current language" will be selected. In this article you will see how to set a custom sort order using the SharePoint Object model.

Procedure

  1. Open Visual Studio 2012 (Run as administrator).
  2. Go to "File" => "New" => "Project...".
  3. Select "Console Application" in the Visual C# node from the installed templates.
  4. Enter the Name and click on "OK".
  5. In the Solution Explorer, right-click on the References folder and then click on "Add Reference".
  6. Add the following assemblies from hive 15 (C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\15\ISAPI).

    • Microsoft.SharePoint.dll
    • Microsoft.SharePoint.Taxonomy.dll
     
  7. Open the Program.cs file and replace the code with the following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Taxonomy;
    using System.Collections.ObjectModel;

    namespace TaxonomyCodeSamples
    {
       
    class Program
        {
           
    static void Main(string[] args)
            {
               
    using (SPSite site = new SPSite("http://c4968397007/sites/Vijai"))
                {
                    TaxonomySession taxSession =
    new TaxonomySession(site);
                   
    //// Get the term store
                    TermStore termStore = taxSession.TermStores["MMS"];
                   
    //// Get the taxonomy group
                    Group group = termStore.Groups["Group"];
                   
    //// Get the termset
                    TermSet termSet = group.TermSets["Termset"];
                   
    //// Get the particular term for which the child terms has to be sorted
                    Term term = termSet.Terms["Term2"];
                   
    //// 09810eaa-3fb3-4fef-bd91-d9ce7941259d - GUID for Term22
                    //// ca14f1b9-c257-4e99-8e2c-3cc722884b5e - GUID for Term21
                    //// Set the custom sort order
                    term.CustomSortOrder = "09810eaa-3fb3-4fef-bd91-d9ce7941259d:ca14f1b9-c257-4e99-8e2c-3cc722884b5e";  
                   
    //// Commit all the changes
                    termStore.CommitAll();
                }
            }
        }
    }

  8. Hit F5.
  9. You will be able to see the custom sort order as in the following:

    output-taxonomy-terms-in-SharePoint 2013.jpg

Summary

Thus in this article you have seen how to set a custom sort order for the taxonomy terms in SharePoint 2013 using the server object model.

Reference

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.termset.customsortorder(v=office.15).aspx

Up Next
    Ebook Download
    View all
    Learn
    View all