SharePoint – Term Store Custom Sort Order in JavaScript

Terms in the SharePoint Managed Metadata Term Store can be ordered through the Custom Sort Order option in the Custom Sort tab of the Term Store Management UI:

Custom Sort in the Term Store management UI.

Custom Sort in the Term Store management UI.

However, if you pull items from the term store using JavaScript getAllTerms, those terms will be returned alphabetically, not in the custom sort order. That is, a call to…

var terms = termSet.getAllTerms();

…will not maintain ordering you’ve specified with the Custom Sort Order by default. What you should do is include the ‘CustomSortOrder’ field in your load before you execute the query. This looks as follows:

var terms = termSet.getAllTerms();
context.load(terms, 'Include(...,CustomSortOrder, ...)'); // specify list of all required fields
context.executeQueryAsync(function () {
    var termEnumerator = terms.getEnumerator();
    while (termEnumerator.moveNext()) {
        var currentTerm = termEnumerator.get_current();
        var termOrder = currentTerm.get_customSortOrder(); // Include enables this to be called

If a custom order is present, the variable termOrder (i.e. var termOrder in the above example) will include a colon delimited list of the term store item Guids that are children of the current term. This delimited list will be built in the correct Custom Sort Order specified in the Term Store. You can easily parse and work with this list now to get items from the Term Store in the correct Custom Sort Order:

var orders = termOrder.split(':');
for (var i = 0; i < orders.length; i++) {
    var key = orders[i]; // key is the term Guid
    // do something, you're looping through children in the correct Custom Sort Order

That’s all there is to it:

  1. getAllTerms
  2. Provide your Include fields. At the very least for this blog, include CustomSortOrder
  3. Now when you execute the query, the fields you specified will be available in the return objects
  4. Call get_customSortOrder() to pull the field
  5. Split on the delimiter (i.e. ‘:’) and go




Categories: SharePoint, Software Development

Tags: , ,

2 replies

  1. i’m getting null value while calling custom sort order method, cal you please help me here ?

    • Hello.

      Do you perhaps not have the `Use custom sort order selected` option specified for the term on which you are invoking the get_customSortOrder() method? If you’re looping through terms, not all of them may have the option selected. In that case, NULL is a valid return value and should be handled in your code.

      Hope that helps.

Leave a Reply