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

    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:


    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 1585 posts 7101 karma points MVP 6x c-trib
    Dec 23, 2020 @ 08:19

    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).



Please Sign in or register to post replies

Write your reply to: