OrchardCore icon indicating copy to clipboard operation
OrchardCore copied to clipboard

Document how `IContentManager.PublishedAsync` only raises the 'OnPublished' event if the content item is draft

Open SzymonSel opened this issue 9 months ago • 8 comments

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);

SzymonSel avatar Apr 07 '25 10:04 SzymonSel

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.

Piedone avatar Apr 07 '25 11:04 Piedone

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?

SzymonSel avatar Apr 08 '25 04:04 SzymonSel

OnPublishing & OnPublished

hishamco avatar Apr 08 '25 04:04 hishamco

Whichever triggers the Workflow Event "Content Published" ;)

SzymonSel avatar Apr 08 '25 04:04 SzymonSel

As said, use VersionOptions.DraftRequired. I.e.:

var ci = await _contentManager.GetAsync(contentItemId, VersionOptions.DraftRequired);
await _contentManager.UpdateAsync(ci);
await _contentManager.PublishAsync(ci);

Piedone avatar Apr 08 '25 14:04 Piedone

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.

SzymonSel avatar Apr 22 '25 08:04 SzymonSel

Doesn't seem to be, would be a good contrib :).

Piedone avatar Apr 22 '25 19:04 Piedone

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.

github-actions[bot] avatar Apr 24 '25 17:04 github-actions[bot]