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?
-
See if this works for you: blogs.msdn.com/b/pedrorod/archive/2013/01/30/…– AkhoyCommented 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– Colin DeClueCommented Aug 12, 2015 at 15:49
2 Answers
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:
Assuming this is present in your environment as well, a little string manipulation or regex wizardry ought to get you what you need.
-
Nope, doesn't exist. The form's action is just the url. Commented Aug 13, 2015 at 15:17
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
};
}
}
}
}