Reading SharePoint Taxonomy Term Store and getDefaultLabel(lcid)

My App reads the SharePoint Term Store and need to get the label associated with the user's language. I get the user's language and lcid, and then I read all the terms under a certain node in the taxonomy using this code:

... some code to get the Term Store, then Term Group, then Term Set, and finally startTerm

var tsTerms = startTerm.get_terms();
context.load(tsTerms);
context.executeQueryAsync(
    function () {
        var termsEnum = tsTerms.getEnumerator();
        while (termsEnum.moveNext()) {
            var currentTerm = termsEnum.get_current();
            var termName = currentTerm.get_name();
            var userLabel = currentTerm.getDefaultLabel(lcid);
            var userLabelValue = userLabel.get_value();
            console.log ("Label=", userLabel, userLabelValue)
... more code ...

In the while loop, I can get all the attributes of the term I need, except for the label. In other samples I found on the web, to get the default label, my userLabel object would be loaded in the context, then another context.executeQueryAsync is called. All that makes sense, but this would induce a lot of calls to the SharePoint server.

But, when I write to the console the userLabel object, is shows as type SP.Result, and when I open it, I see the label I want under the m_value. So there should be no need to go to the server again. However, the userLabelValue is returned as a 0 - obviously, the get_value() does not work. In MSDN documentation, a SP.Result object type is for internal use only. Is there any way to extract the data that it stores?

I have attached a picture of the console with the object expanded, where we clearly see the m_value = "Contrat", which is the label I need to get to.

console log

Answers:

Answer

Use SP.Taxonomy.Term.getDefaultLabel Method to get the default Label for this Term based on the LCID:

function getTermDefaultValue(termId,lcid,success,failure)
{
    var context = SP.ClientContext.get_current();         
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
    var termDefaultValue = taxSession.getTerm(termId).getDefaultLabel(lcid);
    context.executeQueryAsync(function() {
           success(termDefaultValue);
        },
        failure);
}

Note: SP.Taxonomy.Term.getDefaultLabel method expects locale identifier (LCID) for the label.

Usage

var layoutsRoot = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';    
$.getScript(layoutsRoot + 'sp.taxonomy.js',
   function () {
      var termId = 'dff82ab5-6b7a-4406-9d20-40a8973967dd';   
      getTermDefaultValue(termId,1033,printLabelInfo,printError);
});


function printLabelInfo(label)
{
    console.log(String.format('Default Label: {0}',label.get_value()));
}


function printError(sender,args){
    console.log(args.get_message());
}    
Answer

I was facing the same problem and found a solution. Instead of using getDefaultLabel(lcid), use this:

termSet.getTerm(Termid).getAllLabels(lcid).itemAt(0).get_value();

This, in my opinion, does the same as 'getDefaultLabel' but it works. It may cause a little bit more load than the other function but this one works for me

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.