我想使用 JSOM 检索 SharePoint Online 术语集。我有一个脚本,但是,它不会在 DOM 准备好时自动加载术语集,并且我注意到在检索术语集之前我必须在浏览器上进行硬刷新(crtl+F5)。
我的代码:
<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>