How to get content or media by UDI from IUmbracoContextFactory
Hi all.
Is there a way to get content and media by UDI in custom services?
public class CustomService : ICustomService
{
private readonly IUmbracoContextFactory Context;
public CustomService(IUmbracoContextFactory context)
{
Context = context;
}
public IPublishedContent Content(Udi udi)
{
using (var cref = Context.EnsureUmbracoContext())
{
var cache = cref.UmbracoContext.ContentCache;
var node = cache.GetById(udi); // This do not work!
return node;
}
}
}
var homeNode = Umbraco.AssignedContentItem;
Guid udi = new Guid(homeNode.Key.ToString());
var displayUrl = _helperService.ImageUDi(udi).Value<IPublishedContent>("image").Url;
One strange thing I noticed was all properties have the same UDI as the key, see image attached, hopefully, someone with a better understanding of how the internal work can explain why.
Here is how I did it to get a photo from the !Member object.
Here is how to get the media item UDI.
var photoUdi = member.GetValue
And here is how to get the media using the UDI. I saw examples converting the UDI to a guid then using the guid to get the media item. This is not nesessary. You can use the UDI itself in Umbraco to get the media. Eg.
public IPublishedContent GetMediaByUdi(Udi udi)
{
IPublishedContent media = null;
How to get content or media by UDI from IUmbracoContextFactory
Hi all.
Is there a way to get content and media by UDI in custom services?
Hi Bo Jacobsen
Returns image for me
Regards George
Hi George.
Thanks for answering.
How do you auto convert your Udi to a Guid?
Udi - umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2
Guid - 4fed18d8-c5e3-4d5e-88cf-ff3a5b457bf2
I found a way
Hi Bo Jacobsen
What I do is get the key, which is the Guid.
One strange thing I noticed was all properties have the same UDI as the key, see image attached, hopefully, someone with a better understanding of how the internal work can explain why.
Hope it helps
Regards George
Hi George.
I found a way, but i just wanted to respond to your reply.
An Umbraco UDI consists of three parts: the scheme, the type and a GUID Identifier. For example: umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2.
And since i only have access to the Udi, not the id or the key, then i needed a way to get it.
Hi Guys,
Here is how I did it to get a photo from the !Member object.
Here is how to get the media item UDI.
var photoUdi = member.GetValue
And here is how to get the media using the UDI. I saw examples converting the UDI to a guid then using the guid to get the media item. This is not nesessary. You can use the UDI itself in Umbraco to get the media. Eg.
Hope this helps someone.
Regards
David
Hi David.
Seems like they extended the GetById method to also accept a udi. So the right way nowadays is proberly this.
is working on a reply...