Skip to content

Instantly share code, notes, and snippets.

@vman
Last active July 18, 2018 13:39
Show Gist options
  • Select an option

  • Save vman/51ae46fbb439fbf610c8 to your computer and use it in GitHub Desktop.

Select an option

Save vman/51ae46fbb439fbf610c8 to your computer and use it in GitHub Desktop.
SharePoint Online: Search a Taxonomy term in a TermSet using JSOM
(function () {
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
SP.SOD.registerSod("sp.taxonomy.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.taxonomy.js"));
SP.SOD.executeFunc("sp.taxonomy.js", "SP.Taxonomy.TaxonomySession", runFunc);
});
function runFunc() {
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Name of the Term Store from which to get the Terms.
var termStore = taxSession.getDefaultSiteCollectionTermStore();
//GUID of Term Set from which to get the Terms.
var termSet = termStore.getTermSet("7c16e180-d093-4709-8426-e7997acb4302");
var lmi = SP.Taxonomy.LabelMatchInformation.newObject(context);
//Populate the various properties
lmi.set_termLabel("a"); //search terms.
lmi.set_defaultLabelOnly(true);
lmi.set_stringMatchOption(SP.Taxonomy.StringMatchOption.startsWith);
lmi.set_resultCollectionSize(10); //terms to bring back
lmi.set_trimUnavailable(true);
var terms = termSet.getTerms(lmi);
context.load(terms, 'Include(IsRoot, Id, Name, LocalCustomProperties)');
context.executeQueryAsync(function () {
//success
var termEnumerator = terms.getEnumerator();
while (termEnumerator.moveNext()) {
var currentTerm = termEnumerator.get_current();
console.log(currentTerm.get_name());
}
}, function (sender, args) {
//fail
console.log(args.get_message());
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment