Copied to clipboard

Flag this post as spam?

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


  • Pete 213 posts 285 karma points
    Feb 13, 2013 @ 16:01
    Pete
    0

    Get node by type alias in API

    How do you find the id of a node type by its alias using the API without jquery?

    Can it be accessed directly like in razor e.g Model.AncestorOrSelf().Descendants("MyTypeAlias").First() ?

    Thanks

  • gary 385 posts 915 karma points
    Feb 13, 2013 @ 17:53
    gary
    0

    Hi Pete

    In V6 Mvc it is @CurrentPage.Id (or Model equivalent), but it very much now depends upon the version of Umbraco you are using. It will also depend on what you want to do with the node, once you have it as to the best way forward.

    Even though dated the link below is a very good source to point you in the direction to achieving the call you need. (I believe it was created for 4.7, but is still very useable, even if not 100% in every detail)

    http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Hope it helps

    G

     

  • Lars Ljungqvist 14 posts 34 karma points
    Feb 27, 2013 @ 08:38
    Lars Ljungqvist
    0

    Hi Pete

    If you want to get a node anywhere inside Umbraco (here the property "Name" in node 1105), you can use @Model.NodeById(1105).Name. Hope that this helps you :-)

    Lars

  • Richard Terris 273 posts 715 karma points
    Feb 27, 2013 @ 12:20
    Richard Terris
    0
       //Start on the current category node
        var startNode = umbraco.NodeFactory.Node.GetCurrent();
    //If you want to start at a specific node you can do
      var startNode = new umbraco.Nodefactory.Node(1120); 
    //better yet, define the node in config file
    var startNode = new umbraco.Nodefactory.Node(Convert.ToInt32(ConfigurationManager.AppSettings["CaseStudyRoot"]);
        //Get list of case studies under this category
        IEnumerable<INode> children = startNode.ChildrenAsList.Where(x => x.NodeTypeAlias == "CaseStudy");
  • Pete 213 posts 285 karma points
    Mar 15, 2013 @ 13:08
    Pete
    0

    Thanks richard.

    So you have to use .Where in the api, shame there isn't a direct handler.

    Pete

  • Hendy Racher 861 posts 3838 karma points MVP 2x admin c-trib
    Mar 15, 2013 @ 13:47
    Hendy Racher
    0

    or how about:

    Node firstCaseStudyNode = uQuery.GetNodesByType("CaseStudy").First();

    @Richard, I'd suggest against hardcoding IDs in the config, for example IDs may change then deploying a site via Courier - such nodes would likely have unique document types so easy to find via the API (the above uses XPath internally so nice and quick).

  • Andreas Iseli 150 posts 427 karma points
    Mar 15, 2013 @ 16:35
    Andreas Iseli
    0

    I support Hendy's answer because this ist the fastest why and even cached.

Please Sign in or register to post replies

Write your reply to:

Draft