OrchardCMS/OrchardCore · error · InvalidOperationException

The 'PublishContentTask' failed to retrieve the content…

Error message

The 'PublishContentTask' failed to retrieve the content item.

What it means

PublishContentTask.ExecuteAsync resolves the target via GetContentAsync; if it returns null the task throws this InvalidOperationException. No content item could be resolved from the workflow context or the configured expression, so there is nothing to publish.

Solutions

  1. Correct the ContentItem expression so it resolves an existing content item.
  2. Verify an upstream activity (trigger or task) actually provides the ContentItem in the workflow context.
  3. Add a condition activity checking the value is present before PublishContentTask runs.
  4. Catch InvalidOperationException around workflow execution and log the failing activity name.

Example fix

// before
ContentItem = '{{ Workflow.Input."publisedItem" }}' // typo → null
// after
ContentItem = '{{ Workflow.Input."publishedItem" }}'
Defensive patterns

Strategy: validation

Validate before calling

// Resolve before publishing
var content = await resolveContentFromExpression(workflowContext);
if (content is null)
    throw new InvalidOperationException("PublishContentTask expression resolves to no content item.");
if (!content.HasDraft())
    logger.LogDebug("No draft to publish; the task would Noop.");

Type guard

// C# narrowing
static bool IsPublishable(object value) => value is ContentItem { Published: false or _ } item && item.Latest != null;

Try / catch

try { await workflowManager.TriggerEventAsync(eventName, contentItem); }
catch (InvalidOperationException ex) when (ex.Message.Contains("PublishContentTask") && ex.Message.Contains("failed to retrieve"))
{
    logger.LogWarning(ex, "Publish skipped: no content item resolved.");
}

Prevention

When it happens

Trigger: PublishContentTask with a ContentItem expression evaluating to null, or no ContentItem present in the workflow context at execution time.

Common situations: Wrong expression syntax/property name; a preceding activity failed to place the item in the workflow context; the item referenced by the expression was deleted before the publish step ran.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/e5dd49e0a4f162f0. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/PublishContentTask.cs:32

        IWorkflowScriptEvaluator scriptEvaluator,
        IStringLocalizer<PublishContentTask> localizer)
        : base(contentManager, scriptEvaluator, localizer)
    {
    }

    public override string Name => nameof(PublishContentTask);

    public override LocalizedString DisplayText => S["Publish Content Task"];

    public override LocalizedString Category => S["Content"];

    public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
        => Outcome(S["Published"], S["Noop"]);

    public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
    {
        var content = (await GetContentAsync(workflowContext))
            ?? throw new InvalidOperationException($"The '{nameof(PublishContentTask)}' failed to retrieve the content item.");

        if (!content.HasDraft())
        {
            return Outcome("Noop");
        }

        var contentItem = await ContentManager.GetAsync(content.ContentItem.ContentItemId, VersionOptions.DraftRequired);

        if (contentItem == null)
        {
            if (content is ContentItemIdExpressionResult)
            {
                throw new InvalidOperationException($"The '{nameof(PublishContentTask)}' failed to retrieve the content item.");
            }

            contentItem = content.ContentItem;
        }

View on GitHub (pinned to 4306c0717f)