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".
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
- Open Visual Studio 2012 (Run as administrator).
- Go to "File" => "New" => "Project...".
- Select "Console Application" in the Visual C# node from the installed templates.
- Enter the Name and click on "OK".
- In the Solution Explorer, right-click on the References folder and then click on "Add Reference".
- Add the following assemblies from hive 15 (C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\15\ISAPI).
- Microsoft.SharePoint.Taxonomy.dll
- 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();
}
}
}
}
- Hit F5.
- You will be able to see the custom sort order as in the following:
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