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

2 thoughts on “SharePoint – Term Store Custom Sort Order in JavaScript

    • 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.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s