1

SharePoint 2013 on-prem. I'm trying to work with terms and the termstore using JSOM. I found this tutorial which uses SP.Taxonomy.js and LabelMatchInformation to retrieve terms which match a given starting character.

    var context = SP.ClientContext.get_current();
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
    var termStore = taxSession.getDefaultSiteCollectionTermStore();
    var termSet = termStore.getTermSet("<guid>");

    var lmi = SP.Taxonomy.LabelMatchInformation.newObject(context);
    lmi.set_termLabel("a");
    lmi.set_defaultLabelOnly(false);
    lmi.set_stringMatchOption(SP.Taxonomy.StringMatchOption.startsWith);
    lmi.set_resultCollectionSize(10);
    lmi.set_trimUnavailable(true);

    var terms = termSet.getTerms(lmi);
    context.load(terms, 'Include(IsRoot, Id, Name, LocalCustomProperties)');

In the example he was only using the default labels, but I have a lot of synonyms that I want to match as well. This code works and pulls back the terms I would expect, but it doesn't include the synonyms/other labels.

I also found this tutorial for retrieving all terms from a termset. It also works, but doesn't include the synonyms. In the .load() it includes Labels, but that didn't actually bring back anything called labels, much less the actual labels.

 g_ClientContext.load(g_AllTerms, 'Include(IsRoot, Labels, TermsCount, CustomSortOrder, Id, Name, PathOfTerm, Parent, LocalCustomProperties)');

Does anybody know a definitive documentation for which properties can be specified in the Include()?

I found this unansered thread from over two years ago which suggests that term.getAllLabels(<locale code>) is needed. But that would mean making a lot of network calls. In my case I have between 170 to 200 terms I'm trying to retrieve.

Finally I used the browser debugger on the built-in SharePoint managed metadata entry field.

By making an ajax call to to the following url with a data object I'm able to get matching suggestions and include the Synomynms.

var _url = "https://<site>/_vti_bin/TaxonomyInternalService.json/GetSuggestions";
var _payload = {
    "start": "<string to match>",
    "lcid": 1033,
    "sspList": _termStoreGuid,
    "termSetList": _termSetGuid,
    "anchorId": "00000000-0000-0000-0000-000000000000",
    "isSpanTermStores": false,
    "isSpanTermSets": false,
    "isIncludeUnavailable": false,
    "isIncludeDeprecated": false,
    "isAddTerms": false,
    "isIncludePathData": false,
    "excludeKeyword": false,
    "excludedTermset": "00000000-0000-0000-0000-000000000000"
};
....
$.ajax({
    url: _url,
    type: 'POST',
    headers:_headers,
    data: JSON.stringify(_payload)
})

This seems like the best choice, but also the most undocumented and likely to break. It also looks like it will only bring back 20 items at a time.

Can anybody suggest a good way to work with retrieve all terms from a termset including synonyms/other labels?

1 Answer 1

1

Possible I'm missing something here, but if you are looking for the synonyms I think you'll get them from the get_labels:

    var context = SP.ClientContext.get_current();
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
    var termStore = taxSession.getDefaultSiteCollectionTermStore();
    var termSet = termStore.getTermSet("<guid>");

    var lmi = SP.Taxonomy.LabelMatchInformation.newObject(context);
    lmi.set_termLabel("a");
    lmi.set_defaultLabelOnly(false);
    lmi.set_stringMatchOption(SP.Taxonomy.StringMatchOption.startsWith);
    lmi.set_resultCollectionSize(10);
    lmi.set_trimUnavailable(true);

    var terms = termSet.getTerms(lmi);
    context.load(terms, 'Include( Labels )');

    context.executeQueryAsync( _ => { 

        terms.get_data().forEach( (term) => {
            term.get_labels().get_data().forEach( (t) => { 
                if(t.get_isDefaultForLanguage()) {
                    console.log("value", t.get_value());
                } else {
                    console.log("synonym value", t.get_value());
                }
            });
        });
    }, (s,a) => { console.log("err", a.get_message()); } )
3
  • Wow. It is there in both the LabelMatchInformation and the one that gets all the termsets. But I could never find the methods for getting them out. I guess this goes back to the "How do you get a nice understandable JSON object out of this mess of typed objects."
    – Rothrock
    Feb 27, 2018 at 0:35
  • Can you explain how you know this? I spent many hours searching for this answer, and many hours in my browser debugger trying to figure out if the information was included in the response. But because the content comes back as typed objects I can't just inspect it to find if it is there. I'll post another question, but if you have any tips for how someone who didn't know could figure this out, it would be awesome.
    – Rothrock
    Feb 27, 2018 at 0:45
  • 1
    Hehe, I'm not sure, guess it depends. If I can't find anything in the docs or on "google" I start working in the console and vscode ( intellisense) looking for what could be logical where I could find the data I'm looking for. Like the above I just started looking in the console on the terms.get_data()[0] object. Worst case I start looking in the *.debug.js files, usually starting of using a program like FileSeek Feb 27, 2018 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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