1

I'm trying to use _spFriendlyUrlPageContextInfo to get the term ID for a friendly url. However, if we're anonymous this is always undefined. How can I get this term id while anonymous?

2
  • See if this works for you: blogs.msdn.com/b/pedrorod/archive/2013/01/30/…
    – Akhoy
    Commented Aug 12, 2015 at 15:41
  • @uberz91 Thanks for the link, but the terms already have a url. The actual navigation is working fine, the friendly URL links to the correct page. But _spFriendlyUrlPageContextInfo is undefined Commented Aug 12, 2015 at 15:49

2 Answers 2

1

I do not have access to an anonymous access site to test, but in my Office 365 environment I can see the navigation term buried in the action attribute of the form, along with a few other goodies:

enter image description here

Assuming this is present in your environment as well, a little string manipulation or regex wizardry ought to get you what you need.

1
  • Nope, doesn't exist. The form's action is just the url. Commented Aug 13, 2015 at 15:17
1

There is a variable called _spOriginalFormAction where you can find the term name and id.

Edit: It seems that this variable is not yet set when display templates are rendered. Here is the function that I ended up using:

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
                };
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.