2

I’m using managed metadata to create global navigation on a SharePoint Master Page. I understand that a managed metadata term set can only be applied to a single site collection. To bypass this I’m using JavaScript to directly access the term set from within the Master Page.

The code below outputs all terms from the term set into an unordered list, but it doesn’t differentiate between parent and child terms. I need the child items to be added as unordered lists beneath their parents, so they can be made into dropdowns.

The Master Page:

<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<script src="/programs/clintondev/_catalogs/masterpage/ELSANET-Bootstrap/js/ElsaGlobalNavigation.js"></script>
<div>
    <ul class="ms-core-suiteLinkList" data-bind="foreach: globalMenuItems">
        <li class="ms-core-suiteLink">
            <a class="ms-core-suiteLink-a" data-bind="attr: { href: url }"><span data-bind="text: title"></span></a>
        </li>
    </ul>
</div>
<script>  
    ElsaGlobalNavigation.GlobalNavigation.init('Managed Metadata Service', '4271776f-8958-4e45-a12c-ae120e5d92f1');
</script>  

Linked js file (ElsaGlobalNavigation.js):

Function.registerNamespace('ElsaGlobalNavigation.GlobalNavigation');

ElsaGlobalNavigation.GlobalNavigation.MenuItem = function (title, url, root) {
    this.title = title;
    this.url = url;
    this.root = root;
};

ElsaGlobalNavigation.GlobalNavigation.viewModel = {
    globalMenuItems: new Array()
};

ElsaGlobalNavigation.GlobalNavigation.init = function (termStoreName, termSetId) {
    SP.SOD.executeOrDelayUntilScriptLoaded(function () {
        'use strict';
        var nid = SP.UI.Notify.addNotification("<img src='/_layouts/15/images/loadingcirclests16.gif?rev=23' style='vertical-align:bottom; display:inline-block; margin-" + (document.documentElement.dir == "rtl" ? "left" : "right") + ":2px;' />&nbsp;<span style='vertical-align:top;'>Loading navigation...</span>", false);

        SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
        SP.SOD.executeFunc('sp.taxonomy.js', false, Function.createDelegate(this, function () {
            var context = SP.ClientContext.get_current();
            var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

            var termStore = taxonomySession.get_termStores().getByName(termStoreName); // Managed Metadata Service
            var termSet = termStore.getTermSet(termSetId); // 4271776f-8958-4e45-a12c-ae120e5d92f1
            var terms = termSet.getAllTerms();

            console.log(terms);

            context.load(terms);
            context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
                var termsEnumerator = terms.getEnumerator();
                var menuItems = new Array();

                while (termsEnumerator.moveNext()) {
                    var currentTerm = termsEnumerator.get_current();
                    ElsaGlobalNavigation.GlobalNavigation.viewModel.globalMenuItems.push(new ElsaGlobalNavigation.GlobalNavigation.MenuItem(currentTerm.get_name(), currentTerm.get_localCustomProperties()['_Sys_Nav_SimpleLinkUrl'], currentTerm.get_isRoot()));
                }

                console.log(ElsaGlobalNavigation.GlobalNavigation.viewModel.globalMenuItems);

                ko.applyBindings(ElsaGlobalNavigation.GlobalNavigation.viewModel);
                SP.UI.Notify.removeNotification(nid);
            }), Function.createDelegate(this, function (sender, args) {
                alert('The following error has occured while loading global navigation: ' + args.get_message());
            }));
        }));
    }, 'core.js');
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.