Document how `IContentManager.PublishedAsync` only raises the 'OnPublished' event if the content item is draft
Update: This is the original issue, but this turned out not to be a bug, see the comments. Docs are needed, though.
As opposed to the Publish button in the Admin Dashboard, programmatical publishing of ContentItem doesn't raise the event when item is updated. For new ContentItems it work.
Build: 18567
To recreate bug:
Create Workflow with start event "ContentPublished"
var ci = await _contentManager.GetAsync(contentItemId, VersionOptions.Published);
await _contentManager.UpdateAsync(ci);
await _contentManager.PublishAsync(ci);
If you run this code, then you're updating an already published version, which won't run the PublishedAsync handler event, since that (and PublishingAsync) doesn't run for already published events. For that, you need to create a draft to update and publish first: VersionOptions.DraftRequired.
I looking at the code in OrchardCore Modules and found this snippet to be used.
What is the correct approach to update a ContentItem and publish a new version in a single action and have the OnPublished handler triggered?
OnPublishing & OnPublished
Whichever triggers the Workflow Event "Content Published" ;)
As said, use VersionOptions.DraftRequired. I.e.:
var ci = await _contentManager.GetAsync(contentItemId, VersionOptions.DraftRequired);
await _contentManager.UpdateAsync(ci);
await _contentManager.PublishAsync(ci);
Thanks @Piedone, this works, thank you! Is this documented on https://docs.orchardcore.net? This isn't quite obvious and I would love a read, other than exploring the source code.
Doesn't seem to be, would be a good contrib :).
We triaged this issue and set the milestone according to the priority we think is appropriate (see the docs on how we triage and prioritize issues).
This indicates when the core team may start working on it. However, if you'd like to contribute, we'd warmly welcome you to do that anytime. See our guide on contributions here.