Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lee 95 posts 115 karma points
    Jan 24, 2012 @ 01:46
    Lee
    0

    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!

  • Rodion Novoselov 694 posts 859 karma points
    Jan 24, 2012 @ 06:38
    Rodion Novoselov
    0
    <msxsl:script language="cs#" implements-prefix="local-script">
      <msxsl:assembly name="System.Web"/>
      <![CDATA[
        public static void Redirect(url As String) {
          System.Web.HttpContext.Current.Response.Redirect(url)
        }
      ]]>
    </msxsl:script>
    <xsl:template match="/">
      <xsl:variable name="firstChild" select="$currentPage/*[@isDoc and string(umbracoNaviHide) != '1' and position() = 1]"
      <xsl:choose>
        <xsl:when test="$firstChild">
            <xsl:value-of select=" local-script:Redirect(umbraco.library:NiceUrl($firstChild/@id))"
         </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="umbraco.library:Redirect($currentPage/ancestor::*[@isDoc and @level = 1]/@id)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template> 

    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.

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Jan 24, 2012 @ 10:15
    Tim
    0

    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.

  • Lee 95 posts 115 karma points
    Jan 24, 2012 @ 10:20
    Lee
    0

    Thanks Tim

    Could you possibly give me a bit more info (Sorry I'm new to this!)

  • Rodion Novoselov 694 posts 859 karma points
    Jan 24, 2012 @ 10:24
    Rodion Novoselov
    0

    Actually I'd better implement it with Razor macro since it would be much short in it.

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Jan 24, 2012 @ 12:18
    Tim
    0

    @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!

    :)

  • Lee 95 posts 115 karma points
    Jan 26, 2012 @ 00:51
    Lee
    0

    No... I'm still lost on this one. Any more advice would be greatly received!

  • Funka! 398 posts 661 karma points
    Jan 26, 2012 @ 03:18
    Funka!
    0

    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:

    <%@ 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...
    

    Best of luck!

  • Lee 95 posts 115 karma points
    Jan 26, 2012 @ 04:14
    Lee
    0

    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!?

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Jan 26, 2012 @ 10:23
    Tim
    0

    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? 

Please Sign in or register to post replies

Write your reply to:

Draft