Copied to clipboard

Flag this post as spam?

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


  • Mattia 6 posts 77 karma points
    Jul 06, 2017 @ 10:13
    Mattia
    1

    Guid Issue

    Hi!

    I'm trying to get the GUID of a published node of my umbraco tree in my surface controller.

    I have the node ID.

    After calling (successfully) umbracoHelper.TypedContent(myId) i tried to get my key using GetKey() but it always return 0.

    Any idea? The node is published, i can see his guid in my backend .

    Umbraco version -> 7.6.3.

    Thanks.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 11:40
    Alex Skrypnyk
    1

    Hi Mattia

    Try this code:

    var contentWithKey = content as IPublishedContentWithKey;
    return contentWithKey == null ? Guid.Empty : contentWithKey.Key;
    

    Or add this method to your solution:

    public static Guid GetGuid(this IPublishedContent content)
    {
        var contentWithKey = content as IPublishedContentWithKey;
        if (contentWithKey != null)
            return contentWithKey.Key;
        if (content is PublishedContentWrapped)
        {
            contentWithKey = ((PublishedContentWrapped)content).Unwrap() as IPublishedContentWithKey;
            if (contentWithKey != null)
                return contentWithKey.Key;
        }
        return Guid.Empty;
    }
    

    Thanks,

    Alex

  • Mattia 6 posts 77 karma points
    Jul 06, 2017 @ 12:09
    Mattia
    0

    Already tried, but return contentWithKey == null ? Guid.Empty : contentWithKey.Key; returns always Guid.Empty AKA contentWithKey seems to be null...

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 12:40
    Alex Skrypnyk
    0

    It's not right way to do it, but it works:

    var node = ApplicationContext.Services.ContentService.GetById(1111);
    
    
    var guid = node.GetUdi().Guid;
    
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 06, 2017 @ 14:16
    Sebastiaan Janssen
    2

    No no no no no please do not do this!!

    :-)

    This call goes straight to the database and circumvents the content cache. This is very slow and very bad for performance!

    Please don't do this, ever. Unless you know what you're doing.

    Anyway, have you tried this? https://github.com/Shazwazza/Articulate/blob/master/src/Articulate/Models/PublishedContentExtensions.cs#L52

    It looks similar to the method above, but I can't figure out Mattia what exactly is null for you, the code on which line?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 14:27
    Alex Skrypnyk
    1

    Just tested this method on Umbraco 7.6.3, and it works.

    public static Guid GetGuid(this IPublishedContent content)
    {
        var contentWithKey = content as IPublishedContentWithKey;
        if (contentWithKey != null)
            return contentWithKey.Key;
        if (content is PublishedContentWrapped)
        {
            contentWithKey = ((PublishedContentWrapped)content).Unwrap() as IPublishedContentWithKey;
            if (contentWithKey != null)
                return contentWithKey.Key;
        }
        return Guid.Empty;
    }
    
  • Mattia 6 posts 77 karma points
    Jul 06, 2017 @ 12:41
    Mattia
    0

    Yes, I found the same work-around.... But i would prefer not to user the content service

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jul 06, 2017 @ 15:24
    Nik
    2

    Alex's example where he uses the Unwrap method is what I've also found you have to do. Most of the time GetKey on IPublishedContent doesn't work you have to unwrap it rather than cast it to IPublishedContentWithKey to get it to behave.

Please Sign in or register to post replies

Write your reply to:

Draft