Вопрос

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?

Это было полезно?

Решение

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()); } )
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top