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.
SHARE

Sudheer Gangumolu

I'm a Software Analyst / Developer / Blogger, having good experience in working with different flavors & Customization of SharePoint (i.e.,SP O365 / 2013 / 2010 / 2007) and .Net Apps. Currently, working on SharePoint 2013 / SharePoint O365. Guiding people on my technologies.

    Blogger Comment
    Facebook Comment

6 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
  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. 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