Copied to clipboard

Flag this post as spam?

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


  • Andrew Gibson 16 posts 106 karma points
    5 days ago
    Andrew Gibson
    0

    Quickly convert id to a content reference

    Hello, apologies that this is a simple question but it has been bugging me. As an example, I have this:

    <p><a href="@Umbraco.NiceUrl(1243)">Events Search</a></p>
    

    Using the id number is a poor way to do this. The content link is "/about-us/events".

    How can I make easy reference to the content, primarily as a link to the content, without mentioning the id please?

    In several other places I've explored the Umbraco content, got the Alias, taken a number of different approaches... but I still think I'm missing a simple, direct, approach.

  • Paul Seal from codeshare.co.uk 246 posts 1126 karma points c-trib
    5 days ago
    Paul Seal from codeshare.co.uk
    100

    What about finding the item by document type alias?

    Maybe something like this:

    IPublishedContent homePage = Model.Content.AncestorOrSelf("home");
    IPublishedContent searchPage = homePage.Children.Where(x => x.DocumentTypeAlias == "search").FirstOrDefault();
    
    <p><a href="@searchPage.Url">Events Search</a></p>
    
  • Alex Brown 72 posts 390 karma points
    5 days ago
    Alex Brown
    1

    Like you've said, Document Types can be used to find content. That's typically how I do it, as ids are likely to change when publishing to different environments.

    Another way to do it would be to store content ids in the node you're currently residing in. E.g. using content pickers etc.

  • Andrew Gibson 16 posts 106 karma points
    5 days ago
    Andrew Gibson
    0

    Thank you both.

    I've used both approaches described, and both make sense.

    Yes, searching by DocumentTypeAlias, and the first of these, aligns with the way Umbraco works.

    Another way to do it would be to store content ids in the node you're currently residing in. E.g. using content pickers etc.

    I wanted to avoid this, as it is just a single link that I'm putting in a partial.

    I suppose, if I'm looking for a quick fix then there is no difference between using the id and hard-coding "/about-us/events", so I might as well use the more descriptive (hard-coded) version.

    Thank you.

  • Paul Seal from codeshare.co.uk 246 posts 1126 karma points c-trib
    5 days ago
    Paul Seal from codeshare.co.uk
    0

    If it was me I would not use the hard coded path.

    The url is far more likely to change than the id.

    If you don't want to get it by doc type then do what Alex says and store it in the App Settings.

  • Andrew Gibson 16 posts 106 karma points
    5 days ago
    Andrew Gibson
    0

    I'm not quite sure what you mean by App Settings (in Umbraco terms)? If I had a Document Type I could add a property to it, which is what I thought Alex meant, but I'm in a free-standing Partial View.

    Do you mean in a config file?

  • Alex Brown 72 posts 390 karma points
    5 days ago
    Alex Brown
    0

    Sorry I meant adding a content picker to a node. But this is typically more useful for navbars or something.

    I'd search on document type (which is what I always do) since like Paul said, the url may change. If you accidentally change the name of the node, it'll change the url too.

  • Andrew Gibson 16 posts 106 karma points
    21 hours ago
    Andrew Gibson
    0

    Thank you both.

  • Michaël Vanbrabandt 460 posts 2061 karma points
    21 hours ago
    Michaël Vanbrabandt
    0

    What I always use in this type of cases is creating an extensions class which adds extension methods to the UmbracoHelper.

    Then using the TypedContentSingleAtXPath method I take the correct content node:

    public static class UmbracoExtensions
    {
        public static IPublishedContent GetContact(this UmbracoHelper helper)
        {
            return helper.TypedContentSingleAtXPath("//home/contact");
        }
    }
    

    Above examples takes the first node of alias contact under the root home node.

    Hope this helps!

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft