OrchardCMS/OrchardCore · error · ArgumentException
Content field name contains invalid characters
Error message
Content field name contains invalid characters
What it means
ContentPartDefinitionBuilder's field builder Build throws ArgumentException when the field name differs from its ToSafeName() form (case-insensitive), i.e. it contains characters disallowed in safe names.
Solutions
- Sanitize with ToSafeName() before WithField(): builder.WithField(raw.ToSafeName(), ...).
- Strip invalid characters (keep letters/digits, first char a letter) before building.
- Validate field names in any custom admin/import UI.
Example fix
// before
partBuilder.WithField("Sales Amount", f => f.OfType<NumericField>()); // throws
// after
partBuilder.WithField("Sales Amount".ToSafeName(), f => f.OfType<NumericField>()); // "SalesAmount" Defensive patterns
Strategy: validation
Validate before calling
string safe = rawFieldName.ToSafeName(); bool ok = string.Equals(rawFieldName, safe, StringComparison.OrdinalIgnoreCase);
Try / catch
try
{
var fieldDef = fieldBuilder.Build();
}
catch (ArgumentException e)
{
_logger.LogError(e, "Field name '{Name}' contains invalid characters", rawFieldName);
} Prevention
- Apply ToSafeName() to field names from user input or imports.
- Keep display names separate from technical field names.
- Validate names in custom admin forms before persisting.
When it happens
Trigger: WithField("Sales Amount", ...) or names with dashes/dots/symbols, then the field Build(); field names taken from external sources without sanitization.
Common situations: Field names with spaces from form input, hyphenated names from external schemas, accented characters in names.
Understand the failure class
Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Content field name must start with a letter
- Content part name must start with a letter
- Content part name contains invalid characters
- Content type name must start with a letter
- Content type name contains invalid characters
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/ecb3be0efb2e3b06.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs:233
private readonly string _fieldName;
public FieldConfigurerImpl(ContentPartFieldDefinition field, ContentPartDefinition part)
: base(field)
{
_fieldDefinition = field.FieldDefinition;
_fieldName = field.Name;
_partDefinition = part;
}
public override ContentPartFieldDefinition Build()
{
if (!char.IsLetter(_fieldName[0]))
{
throw new ArgumentException("Content field name must start with a letter", "name");
}
if (!string.Equals(_fieldName, _fieldName.ToSafeName(), StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Content field name contains invalid characters", "name");
}
return new ContentPartFieldDefinition(_fieldDefinition, _fieldName, _settings);
}
public override string Name
=> _fieldName;
public override string FieldType
=> _fieldDefinition.Name;
public override string PartName
=> _partDefinition.Name;
public override ContentPartFieldDefinitionBuilder OfType(ContentFieldDefinition fieldDefinition)
{
_fieldDefinition = fieldDefinition;View on GitHub (pinned to 4306c0717f)