Copied to clipboard

Flag this post as spam?

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


  • Jacob 39 posts 88 karma points
    Nov 17, 2010 @ 14:08
    Jacob
    0

    Find node from template alias

    Hello.

    I have a working code sample from this forum which finds alle Content nodes with a specific DocumentType by alias

    DocumentType dt = DocumentType.GetByAlias("Mappe");

    Content[] docs = Document.getContentOfContentType(dt);

    What I want to do is find all nodes which use a specific template by the alias of the template, how would I do this?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 17, 2010 @ 18:34
    Lee Kelleher
    2

    Hi Jacob,

    Quick example using XSLT, but this relies on knowing the template's ID - not the alias name.

    <xsl:for-each select="umbraco.library:GetXmlNodeByXPath('//*[@template = 1234]')">
        <xsl:value-of select="@nodeName" />
    </xsl:for-each>

    There's probably a way of doing this in C# using Linq2Umbraco ... or at an extreme a direct database call?

    Cheers, Lee.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 17, 2010 @ 22:18
    Aaron Powell
    1

    Nope LINQ to Umbraco (at least the NodeDataProvider) doesn't expose template alias, that's not stored in the published cache so it can't

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 18, 2010 @ 11:18
    Lee Kelleher
    0

    I was curious to what the SQL query needed to query the database directly:

    SELECT
        d.nodeId
    FROM
        cmsDocument AS d
        INNER JOIN cmsTemplate AS t ON d.templateId = t.nodeId
    WHERE
        d.published = 1
        AND
        t.alias = 'contentPage'

    Then you would loop through each of the nodeId values, getting a Content or Document object, or even using the nodeFactory?

    Cheers, Lee.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 18, 2010 @ 11:25
    Aaron Powell
    0

    Yeah after thinking more about it the only way to find a template by its alias is to go to the database. The API should be fine if you're not doing it a lot, but be careful if you're using it a lot there'll be lots of DB calls ;).

    If you really need to do it a lot maybe wrap the call in a custom API which you can add caching to.

  • Jacob 39 posts 88 karma points
    Nov 18, 2010 @ 12:32
    Jacob
    0

    Thanks for the input, ill go the db way.

    I will do it so I only have to alter my code in 1 place if the db structure should be changed at some point.

Please Sign in or register to post replies

Write your reply to:

Draft