Hi,
I’m trying to create a package/addon for one of our sites that will allow editors to create a draft link of a page and give this to an external user of the site (doesn’t have a log in to the CMS), which has some security and expiry to it, but hitting a block.
I’ve set up a Render Controller that will work for a “Draft” page type, get the ID (at the moment, just from a parameter, but later checking for a valid Draft) which then gets the preview convent version of the page out and renders that.
I was looking around the code and found the IPublishedContentCache.cs at v13/contrib · umbraco/Umbraco-CMS interface, which has GetById(bool preview, int id) which returns the IPublishedContent, and seems perfect for getting the previewing content page out.
The plan after that is to get the Template, and render that instead.
However, I’m then getting InvalidOperationException: Unable to resolve service for type 'Umbraco.Cms.Core.PublishedCache.IPublishedContentCache' while attempting to activate 'Draft.Controllers.DraftController' when trying to load the page.
I’ve also tried the IPublishdCache but getting the same error.
Has anyone else tried getting this service out and it being injected in a controller?
I’m using Umbraco 13.7.0
Thanks for the suggestion Mats.
I’d prefer to have something that uses an service in the application. rather than calling an API endpoint to the same application though, if that makes sense.
There are certainly times when you can’t get around API requests in controllers (such as linking to a external system), but since this is for an Umbraco site (stand alone, not on the Cloud) then I’d want to get something from the cache/db.
It’s a lot harder to do this properly yourself than just using the exact API endpoint that’s made for this
But if you want to try it yourself, you probably need the IPublishedSnapshotAccessor instead of IPublishedContentCache as Umbraco don’t register this one in the DI container directly.
And then something like:
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var snapshot))
{
var content = snapshot.Content.GetById(true, id);
}
Not sure if it will work as your frontend user might need to be in preview mode for this to work. I would recommend the content delivery API method instead to avoid that problem if it occurs
HMO, what if they did have a login. Then you could just automate the built-in preview mode.
First create a locked down backoffice user just for previews.
Then have your controller log the user in programmatically (no need to actually use username/password here), doing any extra auth (OTP, whatever) beforehand. Finally have your controller redirect to https://example.com/umbraco/preview/?id={page id}.
In v15+ but there’s a proper IPreviewService so you can roll whatever logic you want to enable and/or tell Umbraco whether it should be in preview mode or not.
That is a fantastic solution. You know when you get lost in the forest sometimes because of all the trees
I suppose for safety/not letting get too far in, we won’t give them access to any of the modules but that would serve the purpose well.
To answer the above, it is a V13 site unfortunately. It would be great to get it into Bellisima soon, but we have a fair few custom packages on the site, so that’s going to take some time before it’s ready to be upgraded.