2

I'm trying to pull back the labels from a term store for a different locale in SharePoint Online. So I have installed the different language packs in the term store:

enter image description here

And then I have added a TermSet named "Test" with the label "Test 1" in English and "Hungarian 1" in Hungarian. Good Hungarian translation, dont you think?

So now from JSOM I would like to pull back Hungarian 1. This seems simple enough on paper. But in practice doesnt work. So my JSOM code is:

    var context = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
var termSets = termStore.getTermSetsByName("Test", "1038"); //tried this with an int instead of a string as well

context.load(taxonomySession);
context.load(termStore);
context.load(termSets);

context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {

var termSetsEnumerator = termSets.getEnumerator();

while(termSetsEnumerator.moveNext())
{
    getTermSets(termSetsEnumerator, context, success, error);
}

}), Function.createDelegate(this, function (sender, args) {
    errorCallback('Error: ' + args.get_message());
}));

Unless I provide the LCID for English 1033 I dont get any termsets back. Am I doing something wrong? I can see one other individual has had this problem and the suggested solution works but it means doing an executeQueryAsync for EVERY term by using this code:

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);

https://stackoverflow.com/questions/24098072/reading-sharepoint-taxonomy-term-store-and-getdefaultlabellcid

This surely cannot be right? There must be a way of getting all the terms in 1 language in a bulk collect? The above solution is not viable when you're trying to build a term tree in a different language.

Edit

To show was I mean about what value I expect to get back in English I may have set this value for the label:

enter image description here

But then in (for example) Danish I have Danish on the end, when I pull back the term set name for the Danish LCID and drill into the terms I'd expect to get this label (Test 2 Term Danish), instead I still get the English version of the term (Test 2 Term):

enter image description here

3

Basically it depends on the following settings:

  • a Default Language of Term Store
  • selected language while creating Term Set

Assume English(1033), Finnish(1035) and Russian(1049) languages are installed and the following code is used for retrieving term sets:

var ctx = SP.ClientContext.get_current();         
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);  
var termStore = taxSession.getDefaultSiteCollectionTermStore();

var termSets = termStore.getTermSetsByName("Document Type",1033);  //Default Language
var termSetsRU = termStore.getTermSetsByName("Document Type",1049);
var termSetsFI = termStore.getTermSetsByName("Document Type",1035);
ctx.load(termSets);
ctx.load(termSetsRU);
ctx.load(termSetsFI);


ctx.executeQueryAsync(
        function() {
            console.log(String.format('TermSet for default(English) language {0}',termSets.get_count() > 0 ? 'found' : 'not found'));
            console.log(String.format('TermSet for Finnish language {0}',termSetsFI.get_count() > 0 ? 'found' : 'not found'));
            console.log(String.format('TermSet for Russian language {0}',termSetsRU.get_count() > 0 ? 'found' : 'not found'));
        },
        function(sender,args){
            console.log(args.get_message());
        });

Results

Assume the default language for term store is set to 1033 and we need to create a Term Set named Document Type

Example 1

Selected language: English

Output:

TermSet for default(EN) language found
TermSet for RU language not found
TermSet for FI language not found

Example 2

Selected language: Finnish

Output:

TermSet for default(EN) language found
TermSet for RU language not found
TermSet for FI language found

To summarize:

Update: How to load single Term with Labels info

The following example demonstrates how to:

  • retrieve Term with Labels
  • print default labels for a Term

Example:

function loadTermWithLabels(termId,success,error)
{
    var ctx = SP.ClientContext.get_current();         
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);  
    var termStore = taxSession.getDefaultSiteCollectionTermStore();
    var term = termStore.getTerm(termId);  
    ctx.load(term,'Labels');

    ctx.executeQueryAsync(
        function() {
            success(term);
        },
        error);
}


var termId = "term id goes here";
loadTermWithLabels(termId,
    function(term){
        //print default labels
        var labels = term.get_labels(); 
        for(var j = 0; j < labels.get_count();j++){
            var label = labels.getItemAtIndex(j);  
            if(label.get_isDefaultForLanguage()){  /* Is default label? */
                switch(label.get_language()){
                    case 1033:
                       console.log(String.format('Label for EN language: {0}',label.get_value()));
                       break;
                    case 1049:
                       console.log(String.format('Label for RU language: {0}',label.get_value()));
                       break;
                    case 1035:
                       console.log(String.format('Label for FI language: {0}',label.get_value()));
                       break;      
                }
            }
        }    
    },
    function(sender,args){
       console.log(args.get_message());
    }
);

Update 2: how to load multiple terms with Labels info

The following example demonstrates how to load all the terms in Term Set with Labels info:

function loadTermsWithLabels(termSetId,success,error)
{
    var ctx = SP.ClientContext.get_current();         
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);  
    var termStore = taxSession.getDefaultSiteCollectionTermStore();
    var termSet = termStore.getTermSet(termSetId);
    var terms = termSet.getAllTerms();  
    ctx.load(terms,'Include(Name,Labels)');
    ctx.executeQueryAsync(
        function() {
            success(terms);
        },
        error);
}


var termSetId = "e8c3e26c-e559-4916-a40c-c8087b236836";
loadTermsWithLabels(termSetId,
  function(terms){
     for(var i = 0; i < terms.get_count();i++){
        var term = terms.getItemAtIndex(i);    
        console.log(String.format('Term : {0}',term.get_name()));
        //print labels
        var labels = term.get_labels(); 
        for(var j = 0; j < labels.get_count();j++){
            var label = labels.getItemAtIndex(j);  
            if(label.get_isDefaultForLanguage()){  /* Is default label? */
                switch(label.get_language()){
                    case 1033:
                       console.log(String.format('Label for EN language: {0}',label.get_value()));
                       break;
                    case 1049:
                       console.log(String.format('Label for RU language: {0}',label.get_value()));
                       break;
                    case 1035:
                       console.log(String.format('Label for FI language: {0}',label.get_value()));
                       break;      
                }
            }
        }
     }       
  },
  function(sender,args){
       console.log(args.get_message());
  }
);
  • 1
    Hi Vadim, Thank you for the reply. The code you put above gets a count for me too but if you then dig into that term set and pull out the terms name the label is still the English version, even though I have added a Finish label. So in your example I'll still get back "Test 1" from both termSets and termSetsFI when I drill down into the terms within those term sets. Also should the language selected when creating the term set make a difference? As if I created a "Test" term set in English and switch to Finish it will be there as "Test" as well, I cant create that term set in another language. – SharePoint Dev Jan 14 '15 at 13:09
  • I've edited my answer to try and be a bit clearer about what I'd expect to get back, with your solution when you dig into the term sets do you get the correct label for the LCID you specified when you did the call to getTermSetsByName? As thats where the problem is for me. – SharePoint Dev Jan 14 '15 at 13:19
  • Thanks, with screenshot it became a bit clearer, at least right now i could see you actually need to retrieve terms (not term sets) properties (like labels).. will update my answer – Vadim Gremyachev Jan 14 '15 at 13:25
  • The answer has been updated – Vadim Gremyachev Jan 14 '15 at 16:12
  • 1
    Genius :-) I can see Include() being extremely useful in the future. Bit odd how MS own documentation doesnt mention this Include option msdn.microsoft.com/en-us/library/office/…. Thank you for your help, marked as answer. – SharePoint Dev Jan 15 '15 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.