OrchardCMS/OrchardCore · error · ArgumentException

The 'fieldName' can't be null or empty.

Error message

The 'fieldName' can't be null or empty.

What it means

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'.

Solutions

  1. Provide a non-empty technical field name (it will be safe-named downstream).
  2. Validate/generate the name before the call; derive it from the display name with ToSafeName() if needed.
  3. If only a display name is known, pass it as both fieldName and displayName after sanitizing.

Example fix

// before
await contentDefinitionService.AddFieldToPartAsync(null, "Rating", "NumericField", "Product");
// after
await contentDefinitionService.AddFieldToPartAsync("Rating", "Rating", "NumericField", "Product");
Defensive patterns

Strategy: validation

Validate before calling

// C#
if (string.IsNullOrWhiteSpace(fieldName))
    throw new ArgumentException("Field name is required.", nameof(fieldName));

Type guard

static bool HasFieldName(string fieldName) => !string.IsNullOrEmpty(fieldName);

Try / catch

try
{
    await contentDefinitionService.AddFieldToPartAsync(fieldName, displayName, fieldTypeName, partName);
}
catch (ArgumentException ex)
{
    logger.LogWarning(ex, "Invalid field name");
}

Prevention

When it happens

Trigger: 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.

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

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs:279

        await _contentDefinitionManager.DeletePartDefinitionAsync(name);

        var context = new ContentPartRemovedContext
        {
            ContentPartDefinition = partDefinition,
        };

        _contentDefinitionEventHandlers.Invoke((handler, ctx) => handler.ContentPartRemoved(ctx), context, _logger);
    }

    public Task AddFieldToPartAsync(string fieldName, string fieldTypeName, string partName)
        => AddFieldToPartAsync(fieldName, fieldName, fieldTypeName, partName);

    public async Task AddFieldToPartAsync(string fieldName, string displayName, string fieldTypeName, string partName)
    {
        if (string.IsNullOrEmpty(fieldName))
        {
            throw new ArgumentException($"The '{nameof(fieldName)}' can't be null or empty.", nameof(fieldName));
        }

        var partDefinition = await _contentDefinitionManager.LoadPartDefinitionAsync(partName);
        var typeDefinition = await _contentDefinitionManager.LoadTypeDefinitionAsync(partName);

        // If the type exists ensure it has its own part.
        if (typeDefinition != null)
        {
            await _contentDefinitionManager.AlterTypeDefinitionAsync(partName, builder => builder.WithPart(partName));
        }

        fieldName = fieldName.ToSafeName();
        var position = GetFieldPosition(partDefinition, fieldName);

        await _contentDefinitionManager.AlterPartDefinitionAsync(partName,
            partBuilder => partBuilder.WithField(fieldName, fieldBuilder => fieldBuilder.OfType(fieldTypeName).WithDisplayName(displayName).MergeSettings<ContentPartFieldSettings>(x => x.Position = position)));

        _contentDefinitionEventHandlers.Invoke((handler, context) => handler.ContentFieldAttached(context), new ContentFieldAttachedContext

View on GitHub (pinned to 4306c0717f)