{"record":{"id":"6abf00740afd910f","repo":"OrchardCMS/OrchardCore","slug":"the-content-item-is-missing-a-contentitemid","errorCode":null,"errorMessage":"The content item is missing a 'ContentItemId'.","messagePattern":"The content item is missing a 'ContentItemId'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs","lineNumber":1002,"sourceCode":"        var context = new CloneContentContext(contentItem, cloneContentItem);\n\n        context.CloneContentItem.Data = contentItem.Data.Clone();\n\n        await Handlers.InvokeAsync((handler, context) => handler.CloningAsync(context), context, _logger);\n\n        await _session.SaveAsync(context.CloneContentItem);\n\n        await ReversedHandlers.InvokeAsync((handler, context) => handler.ClonedAsync(context), context, _logger);\n\n        return context.CloneContentItem;\n    }\n\n    private async Task<ContentValidateResult> CreateContentItemVersionAsync(ContentItem contentItem, IEnumerable<ContentItem> evictionVersions = null)\n    {\n        if (string.IsNullOrEmpty(contentItem.ContentItemId))\n        {\n            // NewAsync should be used to create new content items.\n            throw new InvalidOperationException($\"The content item is missing a '{nameof(ContentItem.ContentItemId)}'.\");\n        }\n\n        // Initializes the Id as it could be interpreted as an updated object when added back to YesSql\n        contentItem.Id = 0;\n\n        // Maintain modified and published dates as these will be reset by the Create Handlers\n        var modifiedUtc = contentItem.ModifiedUtc;\n        var publishedUtc = contentItem.PublishedUtc;\n        var owner = contentItem.Owner;\n        var author = contentItem.Author;\n\n        if (string.IsNullOrEmpty(contentItem.ContentItemVersionId))\n        {\n            contentItem.ContentItemVersionId = _idGenerator.GenerateUniqueId(contentItem);\n        }\n\n        // Remove previous latest item or they will continue to be listed as latest.\n        // When importing a new draft the existing latest must be set to false. The creating version wins.","sourceCodeStart":984,"sourceCodeEnd":1020,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs#L984-L1020","documentation":"DefaultContentManager.CreateContentItemVersionAsync throws InvalidOperationException when the ContentItem has no ContentItemId. Versioned creation is only for items that already have an identity (loaded or built via NewAsync); a new item must be created through NewAsync so the manager can assign the ID.","triggerScenarios":"Calling IContentManager.CreateContentItemVersionAsync with a ContentItem constructed with 'new ContentItem()' instead of IContentManager.NewAsync, or deserializing/hand-building an item and dropping its ContentItemId, or passing a default-constructed item to SaveContentItemAsync paths that route into version creation.","commonSituations":"Import/migration scripts that build ContentItem objects manually, unit tests constructing content items by hand, custom modules cloning items but clearing ContentItemId, serialization round-trips that omit the id property.","solutions":["Create the item via await _contentManager.NewAsync(\"TypeName\", VersionOptions.Draft) so ContentItemId is assigned.","If versioning an existing item, load it with _contentManager.GetAsync(contentItemId, VersionOptions.*) instead of constructing a new instance.","Check your deserialization/import code preserves the ContentItemId property.","Wrap the call in try/catch InvalidOperationException and fall back to NewAsync for genuinely new items."],"exampleFix":"// before\nvar item = new ContentItem { ContentType = \"Article\" };\nawait _contentManager.CreateContentItemVersionAsync(item);\n\n// after\nvar item = await _contentManager.NewAsync(\"Article\", VersionOptions.Draft);\nawait _contentManager.CreateContentItemVersionAsync(item);","handlingStrategy":"try-catch","validationCode":"if (string.IsNullOrEmpty(contentItem.ContentItemId))\n{\n    // must create via NewAsync before versioning\n}","typeGuard":"bool CanCreateVersion(ContentItem item) => item is not null && !string.IsNullOrEmpty(item.ContentItemId);","tryCatchPattern":"try\n{\n    await _contentManager.CreateContentItemVersionAsync(item);\n}\ncatch (InvalidOperationException)\n{\n    // fall back: create via NewAsync for genuinely new items\n    var fresh = await _contentManager.NewAsync(item.ContentType, VersionOptions.Draft);\n    // copy fields/parts then create\n}","preventionTips":["Always obtain new content items from IContentManager.NewAsync, never 'new ContentItem()'.","Preserve ContentItemId in import/serialization code.","Add a guard helper that asserts ContentItemId before calling version APIs."],"tags":["content-management","invalid-state","orchardcore"],"backgroundTag":"missing-required-argument","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"}