1

I am trying to get the Label(Title - shown in the Navigation UI) of a term through JSOM. I have successfully got the ID of the term.

Now I got stuck in getting the Label of term using ID. I have pasted my code below. Any suggestions, what am I missing here?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script>
$(document).ready(function(){  

    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/"; 
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", function(){            
                $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
            });
        }
    );
});

function execOperation(){

    var termSetName = "ADLA";
    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();

    clientContext.load(taxonomySession);
    clientContext.load(termStore);
    clientContext.load(termSet);
    clientContext.load(terms);
    clientContext.executeQueryAsync(function onSuccess() {
        var chaparray={};

        var enumerator = terms.getEnumerator();
        while (enumerator.moveNext()) {
            var spTerm = enumerator.get_current();
            var name = spTerm.get_name();
            var id = spTerm.get_id();


    }
}, function onFailure(args) {
    alert('Error: '+args.get_message());
});
}

</script>

Updated:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script>
$(document).ready(function(){  
var taxonomySession;
var clientContext;
    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/"; 
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", function(){            
                $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
            });
        }
    );
});

function execOperation(){

    var termSetName = "ADLA";
    var locale = 1033; 
    clientContext  = SP.ClientContext.get_current();
    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 chaparray={};

        var enumerator = terms.getEnumerator();
        while (enumerator.moveNext()) {
            var spTerm = enumerator.get_current();
            var name = spTerm.get_name();
            var id = spTerm.get_id();

            getTermDefaultValue(id,1033);

        }


    },
function onFailure(args) {
    alert('Error: '+args.get_message());
});

}

function getTermDefaultValue(termId, lcid) {
    var termDefaultValue = taxonomySession.getTerm(termId).getDefaultLabel(lcid);  
}

</script>

Below is the image.. I need to get the "Chapter-1" text.

Title

var taxonomySession = new TaxonomySession(site);
                var termStore = taxonomySession.DefaultKeywordsTermStore;
                var termStoreGroup = termStore.GetSiteCollectionGroup(site);
                var termSet = termStoreGroup.CreateTermSet("ADLA");
                termSet.IsAvailableForTagging = true;
                termSet.TermStore.CommitAll();
4
  • Did you try spTerm.getDefaultLabel(locale) ? See this post sharepoint.stackexchange.com/questions/117030/…
    – Unnie
    Jul 20, 2016 at 9:38
  • Unnie - Thanks for your comment. I tried the same. But the value is SP.Result {} . I am not sure how to get the Label value from this... I have updated my code in the question.. How to get the label value from this..
    – CHANDRU D
    Jul 20, 2016 at 12:12
  • I have added one more image for better clarification.
    – CHANDRU D
    Jul 20, 2016 at 14:02
  • Any help on this? I guess I am confusing it with label.. Is it the label value I have to get or the title value?
    – CHANDRU D
    Jul 20, 2016 at 20:13

1 Answer 1

1

When you enable Managed navigation for a site , the navigation node title is stored as a local custom property named _Sys_Nav_Title .

So if you want to get this data from JSOM you could use below code:

spTerm.get_localCustomProperties()["_Sys_Nav_Title"];
8
  • Thanks for your reply.. Now i have got the clear idea.. I have tried your code and it works for _Sys_Nav_TargetUrl. But When I try to get "_Sys_Nav_Title", i am getting undefined. I checked in the browser console, for spTerm.get_localCustomProperties() , object is returned with only _Sys_Nav_TargetUrl value. Am i missing something here. Please share your thoughts
    – CHANDRU D
    Jul 21, 2016 at 6:12
  • I tried in another environment and it worked Unnie. Thanks a lot..Now I have one more issue, I added a new term and also modified the Navigation node title for already existing term. Both are not reflecting, though my navigation got updated with the new term and changed Node titles. Also I ran the Taxonomy update scheduler timer job to check this. It is not updated in my Jsom code. any ideas?
    – CHANDRU D
    Jul 21, 2016 at 9:05
  • Even I have deleted the termset and checked. Still the old values are coming while running the JSOM code. Even in the incognito mode , I can see the old terms.. Any ideas?
    – CHANDRU D
    Jul 21, 2016 at 11:54
  • Any suggestions on this?
    – CHANDRU D
    Jul 22, 2016 at 3:36
  • still i am having the same issue.. any ideas?
    – CHANDRU D
    Jul 22, 2016 at 9:25

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.