Last active
July 10, 2017 15:22
-
-
Save jquintozamora/d3361c54ea2b98bd1de10e6343c11fdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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