4

We are working on a SharePoint 2013 on-premise farm. I am trying to return the termset object with ID 'xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'. Here is my script. I add a content editor webpart and insert following lines:

<script type="text/javascript">

$(document).ready(function(){       
    SP.SOD.executeFunc('SP.Runtime.js', 'SP.Runtime', function () {
        SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
            SP.SOD.executeFunc('SP.Taxonomy.js', 'SP.Taxonomy', loadTerms)
        });
    });
});

function loadTerms(){
    SP.SOD.executeFunc('sp.taxonomy.js', 'SP.Taxonomy.TaxonomySession', function () {   
        var termId = 'xxxxx-xxxxx-xxxxx-xxxxx-xxxxx';   
        console.log(termId);
        getTerm(termId,printTermInfo,printError);
    });
}


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

function printError(sender,args){
    console.log(args.get_message());
    console.log('fail');
}
</script>

The problem is it return nothing. No error no console log is found. In the F12 tool I can see runtime.js and taxonomy.js is returned with http 200. I think they are loading correctly. What could be the problem?

  • I may be wrong, but I feel there is something missing in your code. In my coding practice I used to connect to metadata service, get term sets and then term.. – Asad Refai May 20 '16 at 10:01
  • Why are you calling SP.Taxonomy.js twice? Could you share your getTerm code? – Mx. May 20 '16 at 10:18
  • getTerm is a function within SP.Taxonomy.js – Mark L May 22 '16 at 2:44
2

I currently worked with the same requirement, so here i am sharing my solution which i used to get the TermSet object.

$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(LoadAllTerms, 'SP.Taxonomy.js');
});

//loads all existing navigation terms
function LoadAllTerms() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllNavigationTerms);
}

//gets all existing navigation terms
function getAllNavigationTerms() {
    var metaDataServiceName = “” Your Managed MetaData Service Name”;
    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js", function () {
        $.getScript(scriptbase + "SP.js", function () {
            $.getScript(scriptbase + "SP.Taxonomy.js", function () {
                termContext = SP.ClientContext.get_current();
                var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(termContext); //Current Taxonomy Session
                var termStores = taxSession.get_termStores(); //Term Stores                        
                termStore = termStores.getByName(metaDataServiceName); //Name of the Term Store from which to get the Terms.                       
                navigationTermSet = termStore.getTermSet("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX"); //GUID of Term Set from which to get the Terms.
               // navigationTermSet is your termset object
           });
        });
    });
}

The above code is working fine for me.

Let me know if you need some more assist.

3

I have used below function to get terms from the term set. You can inspire from it.

First connect to SharePoint Taxonomy session, get hold hold of Default Term Store.

function customGetTerm() {

    var termSetName = "Your Term Set Name",
    locale = 1033,
    clientContext = SP.ClientContext.get_current(),
    taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext),
    termStore = taxonomySession.getDefaultSiteCollectionTermStore(),
    termSets = termStore.getTermSetsByName(termSetName, locale),
    termSet = termSets.getByName(termSetName),
    terms = termSet.getAllTerms(),

    term = terms.getByName("Term Name"),
    childTerms = term.get_terms();

    clientContext.load(childTerms); 
    clientContext.executeQueryAsync(function onSuccess() {
        var enumerator = childTerms.getEnumerator();
        while (enumerator.moveNext()) {
            var spTerm = enumerator.get_current();

        }

    }, function onFailure(args) {
        alert('Error');
    });
}

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.