but in Umbraco 10 the IUrlProvider interface requires an IPublishedContent instance. Interestingly enough, the DefaultUrlProvider's implementation only uses the IPublishedContent.Id property so an overload which accepted just the content Id would work fine (but alas doesn't exist anymore. why?).
Note, the assumption here is that the IContent instance is not published (although it might be).
One work-around would be to create an "empty" IPublishedContent instance with just the Id property assigned (how do I do that, is there a suitable/lightweight class implementing IPublishedContent somewhere in the framework?).
Another work-around would be to "fake" the Url by building one using IUrlSegmentProvider.GetUrlSegment() for each of the IContent's parents. Is there a helper method somewhere already doing this?
I'm not sure if this is true but: I believe you should only be able to generate URL's for a published content. When the document is not published, it is not able to route it. They might've removed the id overload because this violates some rules, or causes some problems that we are not aware of.
Maybe go back one step, why are you trying to get URL's for nodes that are not published?
When reading this, keep in mind we are in the middle of the process of migrating an Umbraco 7 multi-national web site to Umbraco 10. We have code which iterates the content tree for various reasons. This code can’t always make assumptions about whether a content node has been published nor not.
Reasons for iterating and processing the content tree:
Export of content tree to spreadsheet format (CSV) for the following reasons: a) In general, for various auditing purposes. b) Pending content – compares the content tree on the Dev site to the Live site to give the user an overview of which nodes (pages) have content pending for transfer/publishing, and to see if any content has been directly modified on the Live site (hopefully not). c) Product pages – we have the need to do special processing on the product hierarchy. Each product has its own content node. An example is importing data from a back end product database. d) SEO – audit pages for search engine optimization
Automatic generation of a Google search compatible sitemap. This includes using Umbraco’s relation service to map related languages (cross references between the pages for our supported language sub-sites)
Content maintentance and bug fixes: a) Sometimes content gets messed up or is incorrectly updated. We then need to make one-time tools which will iterate content and fix the data. b) When we kicked-off a new language/country specific sub-site we started the process by copying the content tree for one of the existing sub-sites. After doing this we needed to do various post-processing to clean up the new sub-site. For instance many pages/properties referenced other pages in the old site and needed to be updated to reference the corresponding pages of the new sub-site. This job can was automated with code.
We also have code which iterates the media tree for various reasons:
Audit media node names, e.g. compare node name to media file name, and ensure names are according to our naming policies
Try to find out where media nodes are used
Review which image types are used, image compression, efficiency, SEO, etc.
How to get the URL from an IContent instance
How do I get (or build) the Url from an IContent instance? In Umbraco 7 the following code worked:
but in Umbraco 10 the IUrlProvider interface requires an IPublishedContent instance. Interestingly enough, the DefaultUrlProvider's implementation only uses the IPublishedContent.Id property so an overload which accepted just the content Id would work fine (but alas doesn't exist anymore. why?).
Note, the assumption here is that the IContent instance is not published (although it might be).
One work-around would be to create an "empty" IPublishedContent instance with just the Id property assigned (how do I do that, is there a suitable/lightweight class implementing IPublishedContent somewhere in the framework?).
Another work-around would be to "fake" the Url by building one using IUrlSegmentProvider.GetUrlSegment() for each of the IContent's parents. Is there a helper method somewhere already doing this?
I'm not sure if this is true but: I believe you should only be able to generate URL's for a published content. When the document is not published, it is not able to route it. They might've removed the id overload because this violates some rules, or causes some problems that we are not aware of.
Maybe go back one step, why are you trying to get URL's for nodes that are not published?
When reading this, keep in mind we are in the middle of the process of migrating an Umbraco 7 multi-national web site to Umbraco 10. We have code which iterates the content tree for various reasons. This code can’t always make assumptions about whether a content node has been published nor not.
Reasons for iterating and processing the content tree:
We also have code which iterates the media tree for various reasons:
Here's some rough code for reimplementing what was (I think) already in Umbraco 7. This is untested and also needs to be cleaned up:
is working on a reply...