{"record":{"id":"9102d4002d52a286","repo":"OrchardCMS/OrchardCore","slug":"the-fieldname-can-t-be-null-or-empty","errorCode":null,"errorMessage":"The 'fieldName' can't be null or empty.","messagePattern":"The 'fieldName' can't be null or empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs","lineNumber":279,"sourceCode":"\n        await _contentDefinitionManager.DeletePartDefinitionAsync(name);\n\n        var context = new ContentPartRemovedContext\n        {\n            ContentPartDefinition = partDefinition,\n        };\n\n        _contentDefinitionEventHandlers.Invoke((handler, ctx) => handler.ContentPartRemoved(ctx), context, _logger);\n    }\n\n    public Task AddFieldToPartAsync(string fieldName, string fieldTypeName, string partName)\n        => AddFieldToPartAsync(fieldName, fieldName, fieldTypeName, partName);\n\n    public async Task AddFieldToPartAsync(string fieldName, string displayName, string fieldTypeName, string partName)\n    {\n        if (string.IsNullOrEmpty(fieldName))\n        {\n            throw new ArgumentException($\"The '{nameof(fieldName)}' can't be null or empty.\", nameof(fieldName));\n        }\n\n        var partDefinition = await _contentDefinitionManager.LoadPartDefinitionAsync(partName);\n        var typeDefinition = await _contentDefinitionManager.LoadTypeDefinitionAsync(partName);\n\n        // If the type exists ensure it has its own part.\n        if (typeDefinition != null)\n        {\n            await _contentDefinitionManager.AlterTypeDefinitionAsync(partName, builder => builder.WithPart(partName));\n        }\n\n        fieldName = fieldName.ToSafeName();\n        var position = GetFieldPosition(partDefinition, fieldName);\n\n        await _contentDefinitionManager.AlterPartDefinitionAsync(partName,\n            partBuilder => partBuilder.WithField(fieldName, fieldBuilder => fieldBuilder.OfType(fieldTypeName).WithDisplayName(displayName).MergeSettings<ContentPartFieldSettings>(x => x.Position = position)));\n\n        _contentDefinitionEventHandlers.Invoke((handler, context) => handler.ContentFieldAttached(context), new ContentFieldAttachedContext","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs#L261-L297","documentation":"AddFieldToPartAsync requires a non-null, non-empty fieldName because the field's technical name is used to attach, look up, and remove the field on the part. An empty name is rejected immediately with an ArgumentException whose parameter name is 'fieldName'.","triggerScenarios":"Calling AddFieldToPartAsync(null, displayName, fieldTypeName, partName) or AddFieldToPartAsync(\"\", ...) — including the 2-argument convenience overload AddFieldToPartAsync(fieldName, fieldTypeName, partName) that passes fieldName as both name and display name.","commonSituations":"Reading the field name from user input or configuration that was never provided; a variable not initialized before the call; binding an empty form value.","solutions":["Provide a non-empty technical field name (it will be safe-named downstream).","Validate/generate the name before the call; derive it from the display name with ToSafeName() if needed.","If only a display name is known, pass it as both fieldName and displayName after sanitizing."],"exampleFix":"// before\nawait contentDefinitionService.AddFieldToPartAsync(null, \"Rating\", \"NumericField\", \"Product\");\n// after\nawait contentDefinitionService.AddFieldToPartAsync(\"Rating\", \"Rating\", \"NumericField\", \"Product\");","handlingStrategy":"validation","validationCode":"// C#\nif (string.IsNullOrWhiteSpace(fieldName))\n    throw new ArgumentException(\"Field name is required.\", nameof(fieldName));","typeGuard":"static bool HasFieldName(string fieldName) => !string.IsNullOrEmpty(fieldName);","tryCatchPattern":"try\n{\n    await contentDefinitionService.AddFieldToPartAsync(fieldName, displayName, fieldTypeName, partName);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogWarning(ex, \"Invalid field name\");\n}","preventionTips":["Validate the field name at the UI/entry boundary before calling the service.","Derive the technical name from the display name with ToSafeName() when only a label exists.","Never pass form values straight through without an emptiness check."],"tags":["contenttypes","null-argument","fields"],"backgroundTag":"empty-required-field","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"}