Copied to clipboard

Flag this post as spam?

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


  • nickornotto 393 posts 895 karma points
    Apr 18, 2022 @ 14:26
    nickornotto
    0

    How to get Udi for IPublishedContent?

    I can see there is a method to get Udi from IContent:

    content.GetUdi()
    

    Is there a way to get Udi also for IPublishedContent?

  • Paul Seal 511 posts 2804 karma points MVP 5x c-trib
    Apr 18, 2022 @ 17:27
    Paul Seal
    101

    Hi

    First of all you need to get the key from the content item, it is a Guid.

    var contentKey = contentItem.Key;

    Then you need to pass the contentKey into this method to get the Udi:

    var contentUdi = Udi.Create("document", contentKey);

    I hope that helps

    Paul

    (edited to be correct as this was marked as the solution)

  • nickornotto 393 posts 895 karma points
    Apr 18, 2022 @ 19:08
    nickornotto
    0

    I believe you mean

    contentItem.Key
    

    ?

    Because IPublishedContent has no method GetKey()

  • Paul Seal 511 posts 2804 karma points MVP 5x c-trib
    Apr 18, 2022 @ 19:08
    Paul Seal
    0

    Yeah sorry I’m on my mobile. That should be the one.

  • Anders Bjerner 470 posts 2805 karma points MVP 6x admin c-trib
    Apr 18, 2022 @ 19:19
    Anders Bjerner
    101

    Since you specifically mention IPublishedContent, and not IPublishedElement, Paul's example is unfortunately not entirely correct (sorry Poul 😉), as it doesn't account for the underlying type of the IPublishedContent (eg. whether it's content, media etc.).

    The GetKey() method existed prior to Umbraco 9 (I can't remember if it even existed in Umbraco 8). As you found out, it should be the Key property instead.

    Anyways, an IPublishedContent exposes an itemType property indicating the underlying type - so to get the UDI, I would create a method like:

    public Udi GetUdi(IPublishedContent content) {
        if (content == null) throw new ArgumentNullException(nameof(content));
        return content.ItemType switch {
            PublishedItemType.Content => Udi.Create("document", content.Key),
            PublishedItemType.Media => Udi.Create("media", content.Key),
            PublishedItemType.Member => Udi.Create("member", content.Key),
            PublishedItemType.Element => Udi.Create("element", content.Key),
            _ => throw new InvalidOperationException($"Unsupported item type: {content.ItemType}")
        };
    }
    

    This checks against all current item types - except Unknown.

    If you put it in a static class, you can even implement it as an extension method:

    public static Udi GetUdi(this IPublishedContent content) {
        if (content == null) throw new ArgumentNullException(nameof(content));
        return content.ItemType switch {
            PublishedItemType.Content => Udi.Create("document", content.Key),
            PublishedItemType.Media => Udi.Create("media", content.Key),
            PublishedItemType.Member => Udi.Create("member", content.Key),
            PublishedItemType.Element => Udi.Create("element", content.Key),
            _ => throw new InvalidOperationException($"Unsupported item type: {content.ItemType}")
        };
    }
    
  • Paul Seal 511 posts 2804 karma points MVP 5x c-trib
    Apr 18, 2022 @ 19:27
    Paul Seal
    0

    This looks like a great solution

    Sorry for misleading with the getkey and element.

    I’ll have to save this for later.

  • nickornotto 393 posts 895 karma points
    Apr 18, 2022 @ 20:28
    nickornotto
    0

    That is fine, I changed "element" into "document". That I knew.

    Your solution is indeed more comprehensive though. Thanks

  • Søren Kottal 630 posts 4039 karma points MVP 4x c-trib
    Apr 19, 2022 @ 07:59
    Søren Kottal
    0

    Should the signature of this not be public static Udi GetUdi(this IPublishedElement content) {?

    I guess an IPublishedContent will never be an element?

  • Anders Bjerner 470 posts 2805 karma points MVP 6x admin c-trib
    Apr 19, 2022 @ 08:43
    Anders Bjerner
    1

    I also thought about that, but didn't test that far.

    I've put it on my todolist to add the extension method to one of our internal libraries, and a part of that was also looking into whether the signature can be IPublishedElement instead.

  • Søren Kottal 630 posts 4039 karma points MVP 4x c-trib
    Apr 19, 2022 @ 12:28
    Søren Kottal
    1

    Should be in core IMO :)

  • Adam 1 post 72 karma points
    Jun 14, 2022 @ 14:31
    Adam
    0

    Agreed!

    I actually just ran into this on an Umbraco 8 site and found that IPublishedContent actually implements IPublishedElement and it is IPublishedElement that has the Key but IPublishedContent has the ItemType attribute. So it seems, barring actual testing, that this method of getting the UDI only works on IPublishedContent in Umbraco 8. I haven't experimented in 9 or 10 yet so those could be different.

Please Sign in or register to post replies

Write your reply to:

Draft