2

I've a term-set called My Term Set, I need to get the Unique Identifier by passing its name. How can i do that? This is the code i have tried so far but it gives undefined Please help me thanks a lot

Both terms.Guid, terms.id gives undefined

var termSetName = "My Term Set";
var locale = 1033; 

var clientContext = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
var termSets = termStore.getTermSetsByName(termSetName, locale);
var termSet = termSets.getByName(termSetName);
var terms = termSet.getAllTerms();

alert(terms.Guid);
alert(terms.id);

4 Answers 4

4

In your example the methods:

are missing.

The following example demonstrates how to retrieve TermSet by its name:

SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
SP.SOD.registerSod('SP.Taxonomy.TaxonomySession', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
SP.SOD.loadMultiple(['SP.ClientContext', 'SP.Taxonomy.TaxonomySession'], function(){

   getTermSetByName("Document Type",g_wsaLCID,
     function(termSet){
        console.log(termSet.get_id().toString());
     },
     function(sender,args){
        console.log(args.get_message());
     });

});



function getTermSetByName(termSetName,lcid,success,error){
   var ctx = SP.ClientContext.get_current();
   var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
   var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
   var termSets = termStore.getTermSetsByName(termSetName,lcid);
   var termSet = termSets.getByName(termSetName);
   ctx.load(termSet);
   ctx.executeQueryAsync(
     function(){
        success(termSet);
     },
     error);
}
2
  • Thank you for your reply, should i call alert(terms.Guid); alert(terms.id); after the ctx.executeQueryAsync( function(){ success(termSet); }, error); ?? Commented Mar 6, 2015 at 12:03
  • This code worked for me, Thank you for that, But i need to make the termSetID avaiable inside the getTermSetByName function. I tried but i was unable to do so. It just displays in console.log(termset.get_id().toString()); i was unable to return and make that ID avaiable in the getTermSetByName ..please help me on this, Thanks a lot Commented Mar 7, 2015 at 4:54
2

Follow this article, will help you : http://www.vrdmn.com/2012/12/working-with-taxonomy-and-javascript-in.html

as well look at the below code to access the terms:

function execOperation(){

    //Current Context
    var context = SP.ClientContext.get_current();

    //Current Taxonomy Session
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

    //Term Stores
    var termStores = taxSession.get_termStores();

    //Name of the Term Store from which to get the Terms.
    var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");

    //GUID of Term Set from which to get the Terms.
    var termSet = termStore.getTermSet("b49f64b3-4722-4336-9a5c-56c326b344d4");

    var terms = termSet.getAllTerms();

    context.load(terms);

    context.executeQueryAsync(function(){

    var termEnumerator = terms.getEnumerator();

    var termList = "Terms: \n";

        while(termEnumerator.moveNext()){

            var currentTerm = termEnumerator.get_current();

            termList += currentTerm.get_name() + "\n";

        } 

          alert(termList);

    },function(sender,args){

          console.log(args.get_message());

    });

}
1
  • Hi, Is it possible get the id of that termstore just by giving its name..In this example i need to get My Term set ID..Thank you.. Commented Mar 6, 2015 at 9:06
2
// Get Default site collection Term Store ID
                    TaxonomySession taxonomySession = new TaxonomySession(site);
                    TermStore termStore = taxonomySession.DefaultSiteCollectionTermStore;
                    Guid termStoreId = termStore.Id;

this will surely work Thanks

1

I guess you should have tried the following code.

var termSetName = "Your term set name";
var locale = 1033; // your locale. Here is English

var clientContext  = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
var termSets = termStore.getTermSetsByName(termSetName, locale);
var termSet = termSets.getByName(termSetName);
var terms = termSet.getAllTerms();

clientContext.load(taxonomySession);
clientContext.load(termStore);
clientContext.load(termSet);
clientContext.load(terms);

clientContext.executeQueryAsync(function onSuccess() {
    var enumerator = terms.getEnumerator();

    while (enumerator.moveNext()) {
        var spTerm = enumerator.get_current();
        var name = spTerm.get_name();
        var id = spTerm.get_id();
        // ... etc, do your logic for terms
    }
}, function onFailure(args) {
    alert('Error: '+args.get_message());
});

This will definitely work for you.

For more detail read here

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.