5

What I want to do is something like:

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

2 Answers 2

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);
});
4
  • 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
    Commented Oct 1, 2014 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 Commented Oct 1, 2014 at 22:23
  • Success! It's working now. Your original version worked, I forgot to change the term ID. Thanks.
    – matt
    Commented Oct 1, 2014 at 22:25
  • Great, good to know! :) Commented Oct 1, 2014 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

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

Your Answer

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

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