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:
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.
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.
How to get Udi for IPublishedContent?
I can see there is a method to get Udi from IContent:
Is there a way to get Udi also for
IPublishedContent
?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)
I believe you mean
?
Because
IPublishedContent
has no methodGetKey()
Yeah sorry I’m on my mobile. That should be the one.
Since you specifically mention
IPublishedContent
, and notIPublishedElement
, Paul's example is unfortunately not entirely correct (sorry Poul 😉), as it doesn't account for the underlying type of theIPublishedContent
(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 theKey
property instead.Anyways, an
IPublishedContent
exposes anitemType
property indicating the underlying type - so to get the UDI, I would create a method like: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:
This looks like a great solution
Sorry for misleading with the getkey and element.
I’ll have to save this for later.
That is fine, I changed "element" into "document". That I knew.
Your solution is indeed more comprehensive though. Thanks
Should the signature of this not be
public static Udi GetUdi(this IPublishedElement content) {
?I guess an
IPublishedContent
will never be an element?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.Should be in core IMO :)
Agreed!
I actually just ran into this on an Umbraco 8 site and found that
IPublishedContent
actually implementsIPublishedElement
and it isIPublishedElement
that has theKey
butIPublishedContent
has theItemType
attribute. So it seems, barring actual testing, that this method of getting the UDI only works onIPublishedContent
in Umbraco 8. I haven't experimented in 9 or 10 yet so those could be different.is working on a reply...