OrchardCMS/OrchardCore · error · InvalidOperationException

Not the latest version.

Error message

Not the latest version.

What it means

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.

Solutions

  1. Load the item with VersionOptions.Latest before calling UnpublishAsync
  2. Re-fetch the item by ContentItemId with latest flag instead of using a stored instance
  3. Catch the exception and reload the latest version, then retry unpublish

Example fix

// before
var item = await _contentManager.GetAsync(id, VersionOptions.AllVersions);
await _contentManager.UnpublishAsync(item);
// after
var item = await _contentManager.GetAsync(id, VersionOptions.Latest);
await _contentManager.UnpublishAsync(item);
Defensive patterns

Strategy: try-catch

Validate before calling

var latest = await cm.GetAsync(id, VersionOptions.Latest); bool ok = latest is { Latest: true };

Type guard

bool canUnpublish = contentItem is { Latest: true };

Try / catch

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

Prevention

When it happens

Trigger: Calling IContentManager.UnpublishAsync with a ContentItem loaded via VersionOptions.AllVersions/SpecificVersion, or a stale item instance where Latest is false.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs:437

            previous.Published = false;
        }

        contentItem.Published = true;
        await _session.SaveAsync(contentItem, checkConcurrency: true);

        await ReversedHandlers.InvokeAsync((handler, context) => handler.PublishedAsync(context), context, _logger);

        return true;
    }

    public async Task<bool> UnpublishAsync(ContentItem contentItem)
    {
        ArgumentNullException.ThrowIfNull(contentItem);

        // This method needs to be called using the latest version
        if (!contentItem.Latest)
        {
            throw new InvalidOperationException("Not the latest version.");
        }

        ContentItem publishedItem;
        if (contentItem.Published)
        {
            // The version passed in is the published one
            publishedItem = contentItem;
        }
        else
        {
            // Try to locate the published version of this item
            publishedItem = await GetAsync(contentItem.ContentItemId, VersionOptions.Published);
        }

        if (publishedItem == null)
        {
            // No published version exists. no work to perform.
            return true;

View on GitHub (pinned to 4306c0717f)