You may need to manually refresh the cache; if you are trying to use the term immediately after creating it then it may not have had enough time to propagate. If you are using JSOM, try adding a call to termStore.updateCache() in the executeQueryAsync function, like below:.
var termSetId = "77ccf69b-b17e-40cf-8589-7bebc8ee6c9c"
var newGuid = SP.Guid.newGuid().toString();
var context = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxSession.getDefaultSiteCollectionTermStore();
var termSet = termStore.getTermSet(termSetId);
var newTerm = termSet.createTerm('My Fun Term', 1033, newGuid);
context.load(newTerm);
context.executeQueryAsync(function () {
alert("success!");
termStore.updateCache();
}, function (sender, args) {
console.log(args.get_message());
});
Edit
As per @BigRaj's comment, you can ensure that the SP.Taxonomy.js
script is available by wrapping your code in SP.SOD.executeFunc
, as in the below example.
This is basically equivalent to @Anand's answer, but uses SP native methods instead of jQuery.
/* load sp.taxonomy.js before proceeding */
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",
function () {
// Create Terms
});
});