I am trying to put together macroe that would basically check whether the current node has any children, if so it will automatically navigate to the first child, if not, it will redirect to the home page.
I'd recommend moving the redirect function to redirect into an xslt extension rather than having it inline in the XSLT macro. Having code inline like that causes the app to need higher privelidges (so no Medium Trust) and can also causes issue with Macro performance.
We are doing something similar in one of our sections. We have a tiny block of code in our master page which I adapted here for you to get started with:
<%@ Master Language="C#" MasterPageFile="~/masterpages/Master.master" AutoEventWireup="true" %>
<%@ Import Namespace="umbraco.BusinessLogic" %>
<%@ Import Namespace="umbraco.cms.businesslogic.web" %>
<%@ Import Namespace="umbraco.NodeFactory" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
// Based on http://our.umbraco.org/wiki/how-tos/telling-a-page-to-redirect-to-an-external-url
var currentNode = umbraco.NodeFactory.Node.GetCurrent();
if (currentNode.Children.Count > 0)
{
Response.Redirect(currentNode.Children[0].Url);
}
}
</script>
... Rest of your template here...
Check for children, then navigate
Hi
I am trying to put together macroe that would basically check whether the current node has any children, if so it will automatically navigate to the first child, if not, it will redirect to the home page.
Any ideas?
Many thanks!
You will also have to add a namespace to the <xsl:stylesheet> element: xmlns:local-script="urn:local-script" so that this inline extension works.
I'd recommend moving the redirect function to redirect into an xslt extension rather than having it inline in the XSLT macro. Having code inline like that causes the app to need higher privelidges (so no Medium Trust) and can also causes issue with Macro performance.
Thanks Tim
Could you possibly give me a bit more info (Sorry I'm new to this!)
Actually I'd better implement it with Razor macro since it would be much short in it.
@Lee, as Rodion says, you can use a Razor Macro, which you can add the redirect function to without any issues, or for creating an XSLT Extension for Umbraco, see the tutorial here: http://blog.percipientstudios.com/2010/11/12/make-an-app_code-xslt-extension-for-umbraco.aspx it shows how Doug switched functions from inline to App_Code for his awesome XSLT Search project. There's another example of writing a compiled extension here: http://en.wikibooks.org/wiki/Umbraco/Create_xslt_exstension_like_umbraco.Library_in_C
Hope that helps!
:)
No... I'm still lost on this one. Any more advice would be greatly received!
Hi Lee,
We are doing something similar in one of our sections. We have a tiny block of code in our master page which I adapted here for you to get started with:
Best of luck!
Can anyone spot anything here:
@{
var parent = @Model.AncestorOrSelf(2);
var nodes = parent.Children.Where(parent.NodeTypeAlias == "VacancyRedirect");
int totalNodes = nodes.Count();
Response.Write(parent.NodeTypeAlias);
Response.Write(totalNodes.ToString());
if (parent != null) {
if (totalNodes > 0)
{
Response.Redirect(nodes[0].Url);
}
}
I am getting 'problem loading page'. I have a node has a macro container that fires the above code and for some reason it isn't working.
Although interestingly if I change:
if (totalNodes > 0) to if (totalNodes == 0)
totalNodes is the number of nodes attached...... it navigates OK!!
Huh!?
I'd tweak the code slightly, you want to move the null check up a bit, otherwise you could get null reference errors:
@{
var parent = @Model.AncestorOrSelf(2);
if (parent != null) {
var nodes = parent.Children.Where(parent.NodeTypeAlias == "VacancyRedirect");
int totalNodes = nodes.Count();
Response.Write(parent.NodeTypeAlias);
Response.Write(totalNodes.ToString());
if (totalNodes > 0)
{
Response.Redirect(nodes[0].Url);
}
}
Does that work? If not, does it only throw an error on pages with less that 2 ancestors?
is working on a reply...