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
- 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.
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
- 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.
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
- Unable to remove system-defined field.
- The part doesn't exist
- The ' ' field in ' ' part was defined without a specified…
- The 'displayName' can't be null or empty.
- Content type name must start with a letter
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 ContentFieldAttachedContextView on GitHub (pinned to 4306c0717f)