Using SharePoint JavaScript Client Object Model to Get Term Store

In SharePoint 2013 it is very easy to use and get SharePoint taxonomy by using JavaScript Client Object Model.

So, now let us see how to get Term Store Using SharePoint JavaScript Client Object Model step by step.

1. Add a Page in your site and add a Content Editor Web Part in your page.
2. Add JQuery min file by adding reference file in your Site Assets or Add google api reference in Content Editor Web Part.
<script type="text/JavaScript"
  src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
  </script>
3. Load "SP.js" file in your Content Editor Web Part (in SharePoint 2013 it is registered by default).
<script type="text/JavaScript" src="/_layouts/SP.js"></script>

Using SharePoint JavaScript Client Object Model to Get Term Store

$(document).ready(function(){
    LoadAndExecuteSodFunction('sp.js', function () {
        LoadAndExecuteSodFunction('SP.Taxonomy.js', function () {
             var Terms = loadTerms();
         });
    });
});
Click to know How to Use JavaScript Client Object Model

function loadTerms() {
    var TermSetName = "TermSetName";

    //Current Context
    var context = new SP.ClientContext(_spPageContextInfo.siteServerRelativeUrl);
        
    //Get current Site from Current Context
    var site = context.get_site();

    //Load Terms by passing Term Name
    var terms = SP.Taxonomy.TaxonomySession.getTaxonomySession(context)
           .getDefaultSiteCollectionTermStore().getSiteCollectionGroup(site, false)
           .get_termSets().getByName(TermSetName, 1033).get_terms();
    context.load(terms);

    context.executeQueryAsync(onQuerySucceeded(sender, args) {
        var enumer = terms.getEnumerator();

        var TermValues = [];

        while (enumer.moveNext()) {
             var term = enumer.get_current();
             var TermValue = {
                            Name: term.get_name(),
                            Desc: term.get_description(),
                            ID: term.get_id().ToSerialized()
                        };

                        alert("Adding " + TermValue.Name);
                        TermValues.push(TermValue);
                    }
                },
        function onQueryFailed(sender, args) {
              alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
        });

    return {
        loadTerms: loadTerms
    };
};
Note:
If you try to access other properties you see an exception message because other properties are not loaded in this context.

Hope this Article is Helpful for you. Keep checking this space.
Using SharePoint JavaScript Client Object Model to Get Term Store Using SharePoint JavaScript Client Object Model to Get Term Store Reviewed by Sudheer Gangumolu on June 07, 2015 Rating: 5

9 comments:

  1. How to get the page url that is associated with each term?
    And how to get children terms, grandsons terms e so on recursively?

    ReplyDelete
    Replies
    1. Great Article IoT Projects for Students

      Deep Learning Projects for Final Year

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Thanks for share this code.
    I have user above code but I have a error: "_spPageContextInfo is undefined"
    Please cleare me about this error.

    ReplyDelete
  3. How to access children and grand children terms to display in navigation as menu and sub-menus

    ReplyDelete
  4. Hi ,

    how can i access grand child?

    ReplyDelete
  5. How to obtain announcement list title,body and date from that particular list and display it in div tag using javascript?? is it possible??

    ReplyDelete
    Replies
    1. Hi, You can get List Properties and List Items using http://www.sharepointsol.com/2015/05/sharepoint-2013-JSOM-get-lists-items.html and bind it to div.

      Delete
  6. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    Software testing course in chennai

    ReplyDelete