Copied to clipboard

Flag this post as spam?

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


  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 14, 2015 @ 12:45
    Lee Kelleher
    1

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

    var id = GetId("7F09DCDF-68C0-4B06-ABEA-7D683C7D800F");
    var media = Umbraco.TypedMedia(id);
    

    or even if UmbracoHelper accepted GUIDs?

    var media = Umbraco.TypedMedia("7F09DCDF-68C0-4B06-ABEA-7D683C7D800F");
    

    Thanks,
    - Lee

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 14, 2015 @ 13:27
    Lee Kelleher
    101

    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);
    }
    

    Cheers,
    - Lee

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jul 14, 2015 @ 13:30
    Anders Bjerner
    0

    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.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 14, 2015 @ 13:43
    Lee Kelleher
    0

    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

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jul 15, 2015 @ 17:59
    Anders Bjerner
    2

    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:

    IPublishedContent cached = UmbracoContext.ContentCache.GetByGuid("fde43118-30cf-401d-8017-4e4c07ed6d3d");
    
  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jul 14, 2015 @ 13:29
    Anders Bjerner
    1

    To my knowledge GUIDs are not saved in the cache, so your query would involve using either the content or media service:

    IMedia content = UmbracoContext.Current.Application.Services.MediaService.GetById(Guid.Parse("7F09DCDF-68C0-4B06-ABEA-7D683C7D800F"));
    
    IPublishedContent media = content == null ? null : Umbraco.TypedMedia(content.Id);
    

    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.

Please Sign in or register to post replies

Write your reply to:

Draft