2

I'm building a custom navigation solution using the JSOM in Sharepoint 2013.
I need to get the navigation term corresponding to the current page and I found this question with a similar problem and the top-voted answer suggested using _spFriendlyUrlPageContextInfo.

However, on my aspx page, the variable is undefined. What could be the reason, are there any good alternatives?
The site uses managed metadata navigation.

1

The variable is undefined usually when the user is not logged in. I wrote this function to populate the same variable from data existing on the page:

function setFriendlyUrlPageContextInfo() {
    if (typeof (_spFriendlyUrlPageContextInfo) == "undefined" || _spFriendlyUrlPageContextInfo == null) {
        var formaction = null;
        if (_spOriginalFormAction != "undefined" && _spOriginalFormAction != null) {
            formaction = _spOriginalFormAction;
        } else {
            formaction = document.forms.length > 0 ? document.forms[0].action : null;
        }
        if (formaction) {
            var termName = null;
            var termNameMatches = (/\/([^\/]+)\?/ig).exec(formaction);
            if (termNameMatches && termNameMatches.length > 1) {
                termName = termNameMatches[1];
            }
            var termId = null;
            var termIdMatches = (/TermId=([^\&]+)(?:\&|$)/ig).exec(formaction);
            if (termIdMatches && termIdMatches.length > 1) {
                termId = termIdMatches[1];
            }
            if (termName && termId) {
                _spFriendlyUrlPageContextInfo = {
                    title: termName,
                    termId: termId
                };
            }
        }
    }
}
| improve this answer | |

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.