Copied to clipboard

Flag this post as spam?

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


  • Kurniawan Kurniawan 202 posts 225 karma points
    Feb 15, 2011 @ 05:26
    Kurniawan Kurniawan
    0

    Get Current Node Level in .NET Code

    Hi,

     

    I am trying to get a level like in XSLT @level in current node.

    Is this the only way to do it.

    var nodeAsXml = umbraco.library.GetXmlNodeById(currentNode.Id.ToString());
    var nodeLevel = nodeAsXml.Current.GetAttribute("level","");

    Can I get it directly from UmbracoNode ?

     

    Thanks         

  • Sascha Wolter 615 posts 1101 karma points
    Feb 15, 2011 @ 06:57
    Sascha Wolter
    0

    Hi Kruniawan,

    try

     

    var node = Node.GetCurrent();
    var level = node.Level;
    

     

    that should do the trick! :)

    Cheers, Sascha

  • Kurniawan Kurniawan 202 posts 225 karma points
    Feb 15, 2011 @ 07:20
    Kurniawan Kurniawan
    0

    Hi Sasha,

     

    Node doesn't have property Level.

    Is that different from yours ? or you create your extension method ?

  • Sascha Wolter 615 posts 1101 karma points
    Feb 15, 2011 @ 07:29
    Sascha Wolter
    0

    Odd. Which version of Umbraco are you running? I've copied that from my Visual Studio where I've double checked that Node actually has a property called 'Level', I'm on the latest Umbraco version. No custom extension.

    In any case, should you not have the Level property your solution is definitely quite alright as well, depending on how it is implemented internally it might even work faster your way, but I'm out of my depths here I'm afraid...

  • Kurniawan Kurniawan 202 posts 225 karma points
    Feb 16, 2011 @ 00:55
    Kurniawan Kurniawan
    0

    It's weird coz I use version v4.5.2

    and here is the Node factory and there is no Level Property.


    What version you have ?


    namespace umbraco.presentation.nodeFactory
    {
        [Serializable]
        [XmlType(Namespace = "http://umbraco.org/webservices/")]
        public class Node
        {
            public Node();
            public Node(int NodeId);
            public Node(XmlNode NodeXmlNode);
            public Node(int NodeId, bool forcePublishedXml);
            public Node(XmlNode NodeXmlNode, bool DisableInitializing);

            public Nodes Children { get; }
            public DateTime CreateDate { get; }
            public int CreatorID { get; }
            public string CreatorName { get; }
            public int Id { get; }
            public string Name { get; }
            public string NiceUrl { get; }
            public string NodeTypeAlias { get; }
            public Node Parent { get; }
            public string Path { get; }
            public Properties Properties { get; }
            public int SortOrder { get; }
            public int template { get; }
            public DateTime UpdateDate { get; }
            public string Url { get; }
            public Guid Version { get; }
            public int WriterID { get; }
            public string WriterName { get; }

            public DataTable ChildrenAsTable();
            public DataTable ChildrenAsTable(string nodeTypeAliasFilter);
            public static Node GetCurrent();
            public Property GetProperty(string Alias);
        }
    }

  • Sascha Wolter 615 posts 1101 karma points
    Feb 16, 2011 @ 01:27
    Sascha Wolter
    0

    Ah, that explains it. I've got 4.6.1, and I've looked at the source code to find that umbraco.presentation.nodeFactory.Node is now obsolete and that umbraco.NodeFactory.Node should be used instead, which has the Level property (and you are absolutely right, the umbraco.presentation.nodeFactory.Node class doesn't have a Level property as outlined in your post above). So I guess if you don't upgrade to 4.6.1 your best bet is your initial solution!

    Cheers,

    Sascha

  • Seyfullah Tıkıç 23 posts 43 karma points
    Mar 03, 2011 @ 10:09
    Seyfullah Tıkıç
    0

    Hi,

    I need to find current level by using xslt.

    How can I do this?

    Thanks.

  • Kurniawan Kurniawan 202 posts 225 karma points
    Mar 03, 2011 @ 11:09
    Kurniawan Kurniawan
    0

    level is stored in atrribute of it's node in xml. so you can simple take it using @level

  • Seyfullah Tıkıç 23 posts 43 karma points
    Mar 03, 2011 @ 11:28
    Seyfullah Tıkıç
    0

    I have an xslt like this :

    <xsl:param name="currentPage"/>



    <xsl:variable name="level" select="3"/>

    <xsl:template match="/">



    <ul>
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1' and @id != $currentPage/@id]">   
      <li>
        <href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName"/>
        a>
      li>
    xsl:for-each>
    ul>

    xsl:template>

    I want to list all nodes except current node in current level, how can I use current level in this situation.

    <xsl:variable name="currentLevel" select="@level"/>

     

    [@level=$currentLevel]doesn't work.

    Thanks.


  • Sascha Wolter 615 posts 1101 karma points
    Mar 03, 2011 @ 21:23
    Sascha Wolter
    0

    Hi Seyfullah,

    If I understand you correctly you don't necessarily need to use the @level attribute to achieve what you want to do. If you just want to output links to the siblings of the current node you could do something like

    <xsl:variable name="parentOfCurrentPage" select="$currentPage/parent::*[@isDoc]" />

    <xsl:for-each select="$parentOfCurrentPage/*[@isDoc and @id != $currentPage/@id]">

      <a href="{umbraco.library:NiceUrl(current()/@id)}"><xsl:value-of select="current()/@nodeName" /></a>

    </xsl:for-each>

    You can use the level as well, yet the outcome is different since you are also displaying nodes which are at the same level, yet have different parents and are therefore not siblings (just not sure if this is what you want to do):

    <xsl:for-each select="$currentPage/ancestor-or-self::root[@isDoc]/descendant-or-self::*[@isDoc and @level = $currentPage/@level]">

    {{same as above}}

    Hope that helps,

    Sascha

  • Seyfullah Tıkıç 23 posts 43 karma points
    Mar 04, 2011 @ 09:28
    Seyfullah Tıkıç
    0

    First one works great for me.

    Thanks :)

     

  • Filip Witkowski 4 posts 24 karma points
    Dec 03, 2012 @ 23:38
    Filip Witkowski
    0

    I'm using this one:

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <xsl:value-of select="$currentPage/@level"/>

     

    And it works fine (Umbraco 4.7.1 and 4.9)

Please Sign in or register to post replies

Write your reply to:

Draft