{"record":{"id":"e5dd49e0a4f162f0","repo":"OrchardCMS/OrchardCore","slug":"the-publishcontenttask-failed-to-retrieve-the-content-item","errorCode":null,"errorMessage":"The 'PublishContentTask' failed to retrieve the content item.","messagePattern":"The 'PublishContentTask' failed to retrieve the content item\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/PublishContentTask.cs","lineNumber":32,"sourceCode":"        IWorkflowScriptEvaluator scriptEvaluator,\n        IStringLocalizer<PublishContentTask> localizer)\n        : base(contentManager, scriptEvaluator, localizer)\n    {\n    }\n\n    public override string Name => nameof(PublishContentTask);\n\n    public override LocalizedString DisplayText => S[\"Publish Content Task\"];\n\n    public override LocalizedString Category => S[\"Content\"];\n\n    public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)\n        => Outcome(S[\"Published\"], S[\"Noop\"]);\n\n    public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)\n    {\n        var content = (await GetContentAsync(workflowContext))\n            ?? throw new InvalidOperationException($\"The '{nameof(PublishContentTask)}' failed to retrieve the content item.\");\n\n        if (!content.HasDraft())\n        {\n            return Outcome(\"Noop\");\n        }\n\n        var contentItem = await ContentManager.GetAsync(content.ContentItem.ContentItemId, VersionOptions.DraftRequired);\n\n        if (contentItem == null)\n        {\n            if (content is ContentItemIdExpressionResult)\n            {\n                throw new InvalidOperationException($\"The '{nameof(PublishContentTask)}' failed to retrieve the content item.\");\n            }\n\n            contentItem = content.ContentItem;\n        }\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/PublishContentTask.cs#L14-L50","documentation":"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.","triggerScenarios":"PublishContentTask with a ContentItem expression evaluating to null, or no ContentItem present in the workflow context at execution time.","commonSituations":"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.","solutions":["Correct the ContentItem expression so it resolves an existing content item.","Verify an upstream activity (trigger or task) actually provides the ContentItem in the workflow context.","Add a condition activity checking the value is present before PublishContentTask runs.","Catch InvalidOperationException around workflow execution and log the failing activity name."],"exampleFix":"// before\nContentItem = '{{ Workflow.Input.\"publisedItem\" }}' // typo → null\n// after\nContentItem = '{{ Workflow.Input.\"publishedItem\" }}'","handlingStrategy":"validation","validationCode":"// Resolve before publishing\nvar content = await resolveContentFromExpression(workflowContext);\nif (content is null)\n    throw new InvalidOperationException(\"PublishContentTask expression resolves to no content item.\");\nif (!content.HasDraft())\n    logger.LogDebug(\"No draft to publish; the task would Noop.\");","typeGuard":"// C# narrowing\nstatic bool IsPublishable(object value) => value is ContentItem { Published: false or _ } item && item.Latest != null;","tryCatchPattern":"try { await workflowManager.TriggerEventAsync(eventName, contentItem); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"PublishContentTask\") && ex.Message.Contains(\"failed to retrieve\"))\n{\n    logger.LogWarning(ex, \"Publish skipped: no content item resolved.\");\n}","preventionTips":["Validate expression syntax/property names before deploying workflows.","Confirm upstream activities populate the workflow context with the ContentItem.","Use HasDraft checks in condition activities to avoid pointless publish runs.","Log workflow input in development to see what the expression actually resolves to."],"tags":["orchardcore","workflows","contents","null-reference"],"backgroundTag":"entity-not-found","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}