{"record":{"id":"7d0ba1379f8f637b","repo":"OrchardCMS/OrchardCore","slug":"not-the-latest-version","errorCode":null,"errorMessage":"Not the latest version.","messagePattern":"Not the latest version\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs","lineNumber":437,"sourceCode":"            previous.Published = false;\n        }\n\n        contentItem.Published = true;\n        await _session.SaveAsync(contentItem, checkConcurrency: true);\n\n        await ReversedHandlers.InvokeAsync((handler, context) => handler.PublishedAsync(context), context, _logger);\n\n        return true;\n    }\n\n    public async Task<bool> UnpublishAsync(ContentItem contentItem)\n    {\n        ArgumentNullException.ThrowIfNull(contentItem);\n\n        // This method needs to be called using the latest version\n        if (!contentItem.Latest)\n        {\n            throw new InvalidOperationException(\"Not the latest version.\");\n        }\n\n        ContentItem publishedItem;\n        if (contentItem.Published)\n        {\n            // The version passed in is the published one\n            publishedItem = contentItem;\n        }\n        else\n        {\n            // Try to locate the published version of this item\n            publishedItem = await GetAsync(contentItem.ContentItemId, VersionOptions.Published);\n        }\n\n        if (publishedItem == null)\n        {\n            // No published version exists. no work to perform.\n            return true;","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs#L419-L455","documentation":"UnpublishAsync requires the passed ContentItem to be the latest version, since unpublishing operates on the current draft/published lineage. Passing an older version throws InvalidOperationException('Not the latest version.') to prevent corrupting version history.","triggerScenarios":"Calling IContentManager.UnpublishAsync with a ContentItem loaded via VersionOptions.AllVersions/SpecificVersion, or a stale item instance where Latest is false.","commonSituations":"Caching content items and later unpublishing a stale instance; iterating version history and calling UnpublishAsync per version; concurrency where another publish made the instance non-latest.","solutions":["Load the item with VersionOptions.Latest before calling UnpublishAsync","Re-fetch the item by ContentItemId with latest flag instead of using a stored instance","Catch the exception and reload the latest version, then retry unpublish"],"exampleFix":"// before\nvar item = await _contentManager.GetAsync(id, VersionOptions.AllVersions);\nawait _contentManager.UnpublishAsync(item);\n// after\nvar item = await _contentManager.GetAsync(id, VersionOptions.Latest);\nawait _contentManager.UnpublishAsync(item);","handlingStrategy":"try-catch","validationCode":"var latest = await cm.GetAsync(id, VersionOptions.Latest); bool ok = latest is { Latest: true };","typeGuard":"bool canUnpublish = contentItem is { Latest: true };","tryCatchPattern":"try { await cm.UnpublishAsync(item); } catch (InvalidOperationException ex) when (ex.Message == \"Not the latest version.\") { var fresh = await cm.GetAsync(item.ContentItemId, VersionOptions.Latest); await cm.UnpublishAsync(fresh); }","preventionTips":["Always fetch latest version before unpublishing","Never unpublish items from AllVersions enumerations","Refresh cached items before mutating"],"tags":["content-management","versioning"],"backgroundTag":"invalid-state-transition","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}