I want to retrieve SharePoint Online termset using JSOM. I have a script , however, it doesn't automatically load the termset when the DOM is ready and I have noticed that I have to do a hard refresh (crtl+F5) on the browser before the termsets are retrieved.
My code:
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
"use strict";
$(document).ready(function () {
alert("I am ready");
SP.SOD.executeFunc('sp.runtime.js', false, function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
//loads sp.taxonomy.js file
SP.SOD.executeFunc('sp.taxonomy.js', false, GetTermsFromTaxonomyStore);
});
});
function GetTermsFromTaxonomyStore() {
var termSetName1 = "Technology";
var locale1 = 1033; // your locale. Here is English
var clientContext1 = SP.ClientContext.get_current();
var taxonomySession1 = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext1);
var termStore1 = taxonomySession1.getDefaultSiteCollectionTermStore();
var termSets1 = termStore1.getTermSetsByName(termSetName1, locale1);
var termSet1 = termSets1.getByName(termSetName1);
var terms1 = termSet1.getAllTerms();
clientContext1.load(taxonomySession1);
clientContext1.load(termStore1);
clientContext1.load(termSet1);
clientContext1.load(terms1);
clientContext1.executeQueryAsync(function onSuccess1() {
var enumerator1 = terms1.getEnumerator();
while (enumerator1.moveNext()) {
var spTerm1 = enumerator1.get_current();
var spTerm1Val=spTerm1.get_name();
console.log(spTerm1Val);
}
}, function onFailure1(args) {
alert('Error: '+args.get_message());
});
}
});
</script>