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.
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).
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:
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.
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:
This will give you
contentNode
as anIPublishedContent
allowing you to then calltarget.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
is working on a reply...