1

I'm having troubles creating terms in a given term set. I'm using this code enter link description here and is creating the term in the term store but when i try to use the term it's like the field doesn't recognize the term i just created.

Am I missing something?

  • Check, if terms created with valid guid (no uppercase), parent and language? – Falak Mahmood Jun 19 '15 at 1:33
2

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
    });
});
  • 1
    This is the simplest answer, however you should ensure 'SP.Taxonomy.js' is loaded before attempting to call SP.Taxonomy. – BigRaj May 15 at 15:16
  • Right you are, @BigRaj, I have edited my answer to address this. – Chris Odegard May 16 at 21:15
1

Below is the function I use and it creates

  • Creates the new term under root itself (Term set node)
  • Works on any page as all the dependencies are loaded within the script

function create(termName)
{   
    var scriptbase = _spPageContextInfo.siteAbsoluteUrl + "/_layouts/15/";  
    $.getScript(scriptbase + "SP.js", function(){            
        $.getScript(scriptbase + "SP.Taxonomy.js", 
            function()
            {
                var context = new SP.ClientContext.get_current();
                SP.SOD.executeFunc("SP.Taxonomy.js", 'SP.ClientContext', function () {
                    var newGuid = SP.Guid.newGuid();
                    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
                    var termStores = taxSession.get_termStores();
                    var termStore = taxSession.getDefaultSiteCollectionTermStore();
                    var termSet = termStore.getTermSet(<GUID OF TERM SET>);                 
                    var newTerm = termSet.createTerm(termName, 1033, newGuid.toString());                   
                    context.load(newTerm);
                    context.executeQueryAsync(function(){
                        alert("Term Created: " + newTerm.get_name());
                        termStore.updateCache();
                    },
                    function(sender,args){
                        alert(args.get_message());
                    });
                });
            }
        ); 
    });
}
0

I have some code that is creating terms during the PreSave of a list item and then tagging the item with its own term. Seems to be working.

I checked the MS documentation and it doesn't actually say what format the GUID should be in, but it doesn't mention string. In my code I just pass it the object returned by the newGuid method. The rest of the code is the same, but I just pass the guid like this.

...
var newGuid = SP.Guid.newGuid();
var newTerm = parentTerm.createTerm(newTermName,1033,newGuid);
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.