0

Following code does not work correctly

var clientContext = new SP.ClientContext.get_current();
var tSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var ts = tSession.getDefaultSiteCollectionTermStore();
var tset = ts.getTermSetsByName("myTermSet", 1033); 
var terms = tset.get_terms();

It returns an error and says Object doesn't support property or method get_terms

But this code returns all terms correctly

var termSetId = "b48ce920-3051-4e96-b9af-55ddc1fa3f2b";
var clientContext = new SP.ClientContext.get_current();
var tSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var ts = tSession.getDefaultSiteCollectionTermStore();
var tset = ts.getTermSet(termSetId);
var terms = tset.get_terms();

I don't want to use the guid of the term set inside my code. How do I make the first code snippet to work?

1 Answer 1

5

This method getTermSetsByName returns a collection of termsets, not a single termset, so you should probably do something like:

var clientContext = new SP.ClientContext.get_current();
var tSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var ts = tSession.getDefaultSiteCollectionTermStore();
var tset = ts.getTermSetsByName("myTermSet", 1033); 
var terms = tset.getByName("myTermSet")

or get it by index (feels risky):

var clientContext = new SP.ClientContext.get_current();
var tSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var ts = tSession.getDefaultSiteCollectionTermStore();
var tset = ts.getTermSetsByName("myTermSet", 1033); 
var terms = tset.itemAt(0);

I have not tried the codes above, but it should give you an idea.

2
  • thanks, It still does not return any term set.
    – Medes
    Mar 24, 2014 at 12:40
  • And you are executing the code? Mar 24, 2014 at 12:42

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.