Copied to clipboard

Flag this post as spam?

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


  • Alexander Gräf 25 posts 131 karma points
    Dec 23, 2020 @ 05:11
    Alexander Gräf
    0

    Translating umb:// from Content Picker in Macro to actual URL

    I'm currently stuck on getting the URL for a content picker in a macro.

    The selected content gets passed this way:

    umb://document/03169b0b86e94768b0642bba0de24a68

    Unfortunately, all posts about how to translate this to an URL or IPublishedContent seem to use old APIs like ToPublishedContent() or seem to be hacky or require injecting services and multiple calls to them.

    I got as far as getting an IContent object from the umb-URL:

    var target = Udi.Parse(Model.MacroParameters["target"] as string) as Umbraco.Core.GuidUdi;
    var targetNode = Services.ContentService.GetById(target.Guid);
    

    What is the official way to translate the UDI to an URL inside a macro? Pretty sure there has to be an easy and intuitive way.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Dec 23, 2020 @ 08:19
    Nik
    100

    Hi Alexander,

    I'm making the assumption you are in a Macro Partial View?

    You are pretty close to be honest, except you shouldn't be using the Content Service.

    In a Macro Partial View you have access to the Umbraco helper class which is prefect for getting IPublishedContent out.

    So, what I would do is:

    var target = Udi.Parse(Model.MacroParameter["target"] as string);
    var contentNode = Umbraco.Content(target);
    

    This will give you contentNode as an IPublishedContent allowing you to then call target.Url() (Note: If you just access the .Url property and not the .Url() extension method, if you are on a newer version of Umbraco you will get an obsolete warning on it).

    Thanks

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft