Copied to clipboard

Flag this post as spam?

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


  • Tom 711 posts 948 karma points
    Jan 03, 2014 @ 00:30
    Tom
    0

    Get a node by id in umbraco as IPublishedContent or similar?

    Hi I was just wondering how to get node by id in v7 seeing as @Libraray is gone?

    Cheers,

    Tom

  • Dennis Aaen 4433 posts 17887 karma points admin hq c-trib
    Jan 03, 2014 @ 00:38
    Dennis Aaen
    3

    Hi Tom

    Have you tried one of these solution.

    //to return the strongly typed (Umbraco.Core.Models.IPublishedContent) object
    @Umbraco.TypedContent(1234)

    //to return the dynamic representation:
    @Umbraco.Content(1234)

    Or if you want to have multiple ids:

    //to return the strongly typed (IEnumerable) collection 
    @Umbraco.TypedContent(1234, 4321, 1111, 2222)

    //to return the dynamic representation:
    @Umbraco.Content(1234, 4321, 1111, 2222)

    The documentation can be found here: http://our.umbraco.org/documentation/Reference/Mvc/querying

    I hope this can help you.

    /Dennis

  • Aziz Khakulov 9 posts 79 karma points
    Apr 19, 2018 @ 14:04
    Aziz Khakulov
    0

    Is this code for a Razor view? How do I do this in a code, i.e. in a helper class?

  • Kevin Jump 1486 posts 9550 karma points MVP 3x c-trib
    Apr 19, 2018 @ 14:39
    Kevin Jump
    0

    Hi,

    within a helper class you usually won't have access to the UmbracoContext, so you either a) have to create it or b) extend it with your helper functions.

    you can create it

    var umbHelper = new UmbracoHelper(UmbracoContext.Current);
    

    but this is not ideall (because its a refrence based on internal singletons and not always best - there are some long threads on this

    a better way is to extend the class

    as an example :

    public static class MyHelperExtension { 
    
      public static string DoSomethingClever(this UmbracoHelper umbraco, string mything) { 
    
         // umbraco.TypedContent() will work in here
    
      }
    }
    

    then in your razor you can do

    Umbraco.MyHelperExtension("something"); 
    

    and your code will be called and you don't need to create or manage your own Umbraco Helper objects.

  • Aziz Khakulov 9 posts 79 karma points
    Apr 19, 2018 @ 19:47
    Aziz Khakulov
    0

    But I don't want somethin that works in Razor but something that works in a helper library.

  • Kevin Jump 1486 posts 9550 karma points MVP 3x c-trib
    Apr 19, 2018 @ 20:03
    Kevin Jump
    0

    OK,

    Where / when is your helper library going to be called ?

    if you are calling it from a URL (like a REST API call) then you can inherit from the UmbracoApiController class and that has an Umbraco object - Similary the SurfaceController class also has one. and if you want to do something with the backoffice then the UmbracoBackOfficeApiController will also let you get to Umbraco object .

    which road you go down will really depends on how the helper library is being called.

    If you are talking about standalone - not called via IIS as part of any website request eg from a command line - then there is a lot more to do in terms of getting the whole Umbraco application context up and it a bit more of an in depth topic.

Please Sign in or register to post replies

Write your reply to:

Draft