4

I have a termstore like below (sample one)

enter image description here

i need to write a JSOM code to get sub terms under a particular term set As an example once i put My term set's uniquie ID i should reterive My term1,2,3 as my out put

Thank you much.

I followed following code but it does not give proper outcome.

function showTermSets(groupId) {

//we need to load and populate the matching group first, or the term sets that it contains will be inaccessible to our code.

var groupEnum = groups.getEnumerator();

while (groupEnum.moveNext()) {

var currentGroup = groupEnum.get_current();

if (currentGroup.get_id() == groupId) {

context.load(currentGroup);

context.executeQueryAsync(

function(){

//Get Term Sets and load them.

var termSets = currentGroup.get_termSets();

context.load(termSets);

context.executeQueryAsync(

function(){

var termSetEnum = termSets.getEnumerator();

var termSetList = "Term Sets: \n"

while(termSetEnum.moveNext()){

var currentTermSet = termSetEnum.get_current();

var termSetName = currentTermSet.get_name();

var termSetId = currentTermSet.get_id();

termSetList += termSetName + ": " + termSetId + "\n";

}

alert(termSetList);

},

function(){

//Failure loading Term Sets

});

},

function () {

//Failure loading Group.

});

break;

}

}

}

2 Answers 2

8

Please see below code for getting all terms in a termset by using termsetid. The Key thing is making sure SP.js,sp.runtime.js and sp.taxonomy.js are loaded before you make a call to taxonomy store.

 $(document).ready(function () {        
        //This makes sure all necessary Js files are loaded before you call taxonomy store
        SP.SOD.executeFunc('sp.runtime.js', false, function () {
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
                SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));//loads sp.taxonomy.js file
                SP.SOD.executeFunc('sp.taxonomy.js', false, GetTermsFromTaxonomyStore);
            });
        });
    });



    //This method uses the Taxonomy client side object calls to get the terms 
    function GetTermsFromTaxonomyStore() {

        //Current Context
        var context = SP.ClientContext.get_current();
        //Current Taxonomy Session
        var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
        //Term Store under which to create the term.
        var termStore = taxSession.getDefaultSiteCollectionTermStore();
         //Pass ID of the Meetings Term Set
        var termSet = termStore.getTermSet("termsetid here");
        var terms = termSet.get_terms();
        context.load(terms);
        context.executeQueryAsync(function () {
            var termEnumerator = terms.getEnumerator();
            while (termEnumerator.moveNext()) {
                var currentTerm = termEnumerator.get_current();
                console.log(currentTerm.get_name());
            }
        }, function (sender, args) {
            console.log(args.get_message());
        });
    }
4
  • 2
    Thank you for this it is very helpful. How do I enumerate through the children of a term? In the OP there is My Term 1 which has children, how would we see those.
    – Rothrock
    Commented Mar 3, 2015 at 19:53
  • Thank you it is very use full and it work perfectly.. Thank you very much........ Commented Mar 4, 2015 at 6:38
  • One more question, is it possible to get termsetid by giving termset name? Commented Mar 4, 2015 at 6:42
  • 2
    @SharePointLearner msdn.microsoft.com/en-us/library/office/dn269605.aspx . See this link
    – Unnie
    Commented Mar 4, 2015 at 9:52
-2
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script> 
<link href="/sites/Search3/Style%20Library/scripts/taxonomypickercontrol.css" rel="stylesheet"/> 
<script src="/sites/Search3/Style%20Library/scripts/taxonomypickercontrol.js" 
type="text/javascript"></script> 
<script src="/sites/Search3/Style%20Library/scripts/taxonomypickercontrol_resources.en.js" 
type="text/javascript"></script> 

<script>
$(document).ready(function(){     
    var scriptbase = "http://SharePoint/search/_layouts/15/";   
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", function(){
                    $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
            });
        }
    );
});


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("Managed Metadata Service Application");

    //GUID of Term Set from which to get the Terms.
    var termSet = termStore.getTermSet("833e401a-6fcb-476c-8d28-61166689c088");

    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() + ", ";  
        }       
          alert(termList);          
    },function(sender,args){    
          console.log(args.get_message());  
    }); 
}
</script>
1
  • 2
    It is good if you provide the little description about the code so one can understand it easily. Commented Dec 22, 2016 at 5:48

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.