5

What I want to do is something like:

var myTerm = getTermInfo(55c6801f-78b3-485f-b811-530bec1961e9);
  • What version of SharePoint do you use? – Andrew Sep 30 '14 at 19:18
  • SharePoint Server 2013 – matt Sep 30 '14 at 19:24
1

How to retrieve Term via SharePoint JSOM

Use SP.Taxonomy.TaxonomySession.getTerm Method to get a Term object that is based on Term IDs:

function getTerm(termId,success,failure)
{
    var context = SP.ClientContext.get_current();         
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
    var term=taxSession.getTerm(termId);
    context.load(term);
    context.executeQueryAsync(function() {
           success(term);
        },
        failure);
}

Example

var layoutsRoot = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';    
$.getScript(layoutsRoot + 'sp.taxonomy.js',
   function () {
      var termId = 'dff82ab5-6b7a-4406-9d20-40a8973967dd';   
      getTerm(termId,printTermInfo,printError);
});



function printTermInfo(term)
{
    console.log(String.format('Name: {0}',term.get_name()));
}

function printError(sender,args){
    console.log(args.get_message());
}   

Update

Another approach for loading sp.taxonomy.js library:

SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));   
SP.SOD.executeFunc('sp.taxonomy.js', 'SP.Taxonomy.TaxonomySession', function () {   
    var termId = 'dff82ab5-6b7a-4406-9d20-40a8973967dd';   
    getTerm(termId,printTermInfo,printError);
});
  • I'm getting an error with this. SCRIPT5022: Object reference not set to an instance of an object on server. The object is associated with method GetTerm. File: sp.runtime.js, Line: 2, Column: 31379 – matt Oct 1 '14 at 22:04
  • Hmm..i just tested the provided code in SPO and it works just fine for me.. Probably it related with loading of sp.taxonomy.js library, the updated answer contains an alternative way for loading sp.taxonomy.js – Vadim Gremyachev Oct 1 '14 at 22:23
  • Success! It's working now. Your original version worked, I forgot to change the term ID. Thanks. – matt Oct 1 '14 at 22:25
  • Great, good to know! :) – Vadim Gremyachev Oct 1 '14 at 22:26
2

You can use getTerm method in sp.taxonomy.js library.

//Current Context
var context = SP.ClientContext.get_current();         
//Current Taxonomy Session    
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
var term=taxSession.getTerm("id of the term");

See more details here . http://msdn.microsoft.com/en-us/library/office/dn312545(v=office.15).aspx

  • How do you get the default label value from that term? – matt Oct 1 '14 at 18:31

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.