x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar2posts22karma

    Content Picker in u4.7 with Razor

    Chris Compton started this topic February 17, 2012 @ 04:15

    I'm just starting out with Umbraco on a project.  We are starting with 4.7.

    I am using Razor in the templates, and everything was going smoothly until I started looking at how to get the information on a page referenced by a Content Picker.  I'd like to be able to obtain things like the title and nice url.

    I've seen some information in the forums on doing this within u5, but I don't think those examples are suitable for the 4.7 environment.  Any advice on how to approach this?


  • Avatar108posts168karma
    Comment with ID: 108353
    Evan Jardine posted this reply February 17, 2012 @ 06:20

    Hi Chris

    Try using the umbraco.library.NiceUrl(int) method.

    @umbraco.library.NiceUrl(id)

    Cheers

    Evan


  • Avatar132posts200karma
    Comment with ID: 108361
    Ben Norman posted this reply February 17, 2012 @ 07:55

    Get Media item with the following:

    var media = Model.MediaById({id});

    Get the Url for the media item with the following:

    var url = media.UmbracoFile;

     


  • Avatar210posts355karma
    Comment with ID: 108402
    Douglas Ludlow posted this reply February 17, 2012 @ 12:47

    If you're starting out a new project, then you must be using the latest version of Umbraco 4.7, version 4.7.1.1, right? And it sounds like you'd like to get content, not media. The content picker will store the id of the picked node as a string. If the alias of the content picker property is "content", then:

    var node = Library.NodeById(Model.content);

    ...will give you access to the node. node is now a DynamicNode, just like Model, so you have access to all its properties. Refer to the handy cheatsheet. For a link to the node using its title (name) and nice url:

    <a href="@node.NiceUrl">@node.Name</a>

    Hope that helps!


  • Chris Compton posted this reply February 17, 2012 @ 08:57

    The node code was perfect!!  Thank you!

     

    Though I wasn't looking for media, thanks for that information too, because media is my next adventure.

     

     


  • Avatar210posts355karma
    Comment with ID: 108444
    Douglas Ludlow posted this reply February 17, 2012 @ 09:00

    No problem, glad I could help.


  • Stefan van Leusden posted this reply March 2, 2012 @ 04:11

    What do you think about this method?
    It works, but I'm just curious, maybe you would do it a different way :)

     @{
        var site = Model.AncestorOrSelf("Example1");
      
        if (site.HasProperty("**aliasname**") && !String.IsNullOrWhiteSpace(site.**aliasname**.ToString()))
        {
            var bla2 = new DynamicNode(site.**aliasname**);

            <a href="@bla2.NiceUrl">
               We like Umbraco
            </a>
        }
    }

  • Avatar210posts355karma
    Comment with ID: 109753
    Douglas Ludlow posted this reply March 2, 2012 @ 05:14

    I guess it's really user preference, but I'd probably do it this way:

    @{
        var site = Model.AncestorOrSelf("Example1");

        if (site.aliasName.GetType() != typeof(DynamicNull) && site.aliasName != "")
        {
            var bla2 = Library.NodeById(site.aliasName);

            <a href="@bla2.NiceUrl">
                We like Umbraco
            </a>
        }
    }

    It's not really that different... but I like using the Library methods instead of contructors, but that's just me. I also like checking for DynamicNull, which is what an alias would return if the node didn't have the property.


Please login or Sign up To post replies