1

In Sharepoint hosted app, using RestAPI and jquery, we want to create term, by setting the values of description and available for tagging. How will we do this?

1 Answer 1

1

Taxonomy API is not supported via REST service but you could consider to create a term via JSOM API in SharePoint-hosted app as demonstrated below:

var sp_files = [
    'SP.Runtime.js', 
    'SP.js', 
    'SP.Taxonomy.js'
];

var layoutsPath = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getMultiScripts(sp_files, layoutsPath).done(function() {

     var termSetId = 'cb59f0f1-df29-4586-883d-b5f860d89c9f';
     var ctx = SP.ClientContext.get_current();
     var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
     var termStore = taxSession.getDefaultSiteCollectionTermStore(); //retrieve default Term Store
     var termSet = termStore.getTermSet(termSetId);

     //create Term
     var termGuid = SP.Guid.newGuid();
     var termName = "News";
     var newTerm = termSet.createTerm(termName, 1033, termGuid.toString());
     newTerm.setDescription('News term',1033);
     ctx.load(newTerm);
     $.executeQueryPromise(ctx,newTerm)
     .then(function(term){
            console.log("Term has been created");
          },logError);
});


function logError(error){
    console.log(error.get_message());
}

where

//from http://stackoverflow.com/questions/11803215/how-to-include-multiple-js-files-using-jquery-getscript-method
$.getMultiScripts = function(arr, path) {
    var _arr = $.map(arr, function(scr) {
        return $.getScript( (path||"") + scr );
    });

    _arr.push($.Deferred(function( deferred ){
        $( deferred.resolve );
    }));
    return $.when.apply($, _arr);
}    




$.executeQueryPromise = function(ctx, result) {
    result = result || {};
    var d = $.Deferred();
    ctx.executeQueryAsync(function () {
         d.resolve(result);
    }, function (sender, args) {
         d.reject(args);
    });
    return d.promise();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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