In answer to my own question, I had a look through the Umbraco core source and found a method called GetIdForKey on the EntityService instance.
Which means I can do this...
var guid = new Guid("7F09DCDF-68C0-4B06-ABEA-7D683C7D800F");
var attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(guid, Umbraco.Core.Models.UmbracoObjectTypes.Media);
if (attempt.Success)
{
var media = Umbraco.TypedMedia(attempt.Result);
}
Get ID (int) from GUID?
Is it possible to get a node's
int
ID (fro either content or media) from a GUID value?Ideally something like this would be useful...
or even if UmbracoHelper accepted GUIDs?
Thanks,
- Lee
In answer to my own question, I had a look through the Umbraco core source and found a method called
GetIdForKey
on theEntityService
instance.Which means I can do this...
Cheers,
- Lee
Seems like you beat me to the answer, but I don't know of a way do it without using the service layer, which again will hit the database.
Thanks Anders, I appreciate your answer too! #h5yr
With the
EntityService.GetIdForKey
call, it caches the result in the runtime-cache...https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Core/Services/EntityService.cs#L50
But yeah, it's needs to hit the database at least once.
Good tip about indexing GUIDs in Examine, I like it!
Cheers,
- Lee
If going for the EntityService approach, I've created a few extension methods for the content and media services, that will make usage a bit easier:
https://gist.github.com/abjerner/0a8eae7346f9b1c79908
So it could be used like:
To my knowledge GUIDs are not saved in the cache, so your query would involve using either the content or media service:
I assume that GUIDs will be part of the cache in Umbraco 8, but hopefully the HQ will add support for this for Umbraco 7.next as well.
Another approach could be to somehow make sure the GUIDs are indexed in Examine, and then search for them there.
is working on a reply...