Skip to content

Instantly share code, notes, and snippets.

@jquintozamora
Last active July 10, 2017 15:22
Show Gist options
  • Save jquintozamora/d3361c54ea2b98bd1de10e6343c11fdc to your computer and use it in GitHub Desktop.
Save jquintozamora/d3361c54ea2b98bd1de10e6343c11fdc to your computer and use it in GitHub Desktop.
export default class TaxonomyAPI {
/*
* Function to get all terms of a given taxonomy Term Set
*/
public static getAllTermsByTermSet(termSetGuid: string, termSetName: string, showOnlyAdvailableForTag: boolean) {
return new Promise((resolve, reject) => {
SP.SOD.executeFunc("sp.js", "SP.ClientContext", () => {
SP.SOD.registerSod("sp.taxonomy.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.taxonomy.js"));
SP.SOD.executeFunc("sp.taxonomy.js", "SP.Taxonomy.TaxonomySession", () => {
const ctx = SP.ClientContext.get_current();
const taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
const termStore = taxSession.getDefaultSiteCollectionTermStore();
const termSet = termStore.getTermSet(new SP.Guid(termSetGuid));
const terms = termSet.getAllTerms();
ctx.load(terms, "Include(IsRoot, TermsCount, Id, Name, PathOfTerm, IsAvailableForTagging)");
ctx.executeQueryAsync(
() => {
let items: Array<{}> = [];
const termEnumerator = terms.getEnumerator();
while (termEnumerator.moveNext()) {
const currentTerm: any = termEnumerator.get_current();
const termObj: any = {
label: currentTerm.get_name(),
value: currentTerm.get_id().toString()
};
items = [...items, termObj];
}
resolve(items);
},
(sender, args) => {
reject(args.get_message());
});
});
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment