Field
Field
Field is the base class for every field in the Schema of a ContentModelling object. It holds the properties that every field type shares, such as the display name, UID, and data type. Each field type adds its own properties in a subclass, for example TextboxField or ReferenceField.
When to Use This Class: Use a subclass to add a field to a schema. Use Field itself for a field type that has no subclass, or when your code reads the shared properties of every field in a fetched schema.
Conceptual Role: Acts as the polymorphic base type of ContentModelling.Schema, GroupField.Schema, and Block.Schema.
Returns: Field. One field of a content type or global field schema.
Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| DisplayName | string? | No | null | Name of the field in the entry editor. The API requires it. Serialized as display_name. |
| Uid | string? | No | null | Unique ID of the field within the schema. The API requires it. Serialized as uid. |
| DataType | string? | No | null | Data type of the field, for example text, number, or reference. Serialized as data_type. |
| FieldMetadata | FieldMetadata? | No | null | Extra settings such as a default value or instructions. Serialized as field_metadata. |
| Multiple | bool | No | false | Lets the field hold more than one value. Serialized as multiple. |
| Mandatory | bool | No | false | Requires a value in every entry. Serialized as mandatory. |
| Unique | bool | No | false | Requires a value that no other entry uses. Serialized as unique. |
| DisplayType | string? | No | null | Presentation widget for a text field, for example dropdown or checkbox. Serialized as display_type. |
Class-Level Notes
Writing a schema.
- The SDK serializes each field by its runtime type. A subclass sends its own properties at every level of nesting, including fields inside a GroupField or a Block.
- The SDK leaves out a property that is null. It always sends multiple, mandatory, and unique, with false when you leave them unset.
- Set DataType yourself. No subclass sets it for you, and the API reads the field type from it.
Reading a schema: subclass selection.
- client.SerializerOptions holds a converter that creates a subclass for each schema entry it reads. ContentstackResponse.OpenTResponse<T>() uses the same options. Deserializing with your own JsonSerializerOptions returns plain Field objects.
- The converter checks the JSON of each field in the order of the table below and stops at the first match:
| JSON of the field | Class the SDK creates |
|---|---|
| extension_uid has a value | ExtensionField |
| data_type is group | GroupField |
| data_type is blocks | ModularBlockField |
| data_type is reference | ReferenceField |
| data_type is global_field | GlobalFieldReference |
| data_type is extension | ExtensionField |
| data_type is taxonomy | TaxonomyField |
| data_type is number | NumberField |
| data_type is isodate | DateField |
| data_type is file, and the field has dimension or field_metadata.image set to true | ImageField |
| data_type is file otherwise | FileField |
| data_type is json | JsonField |
| data_type is text, and the field has enum or display_type set to dropdown or checkbox | SelectField |
| data_type is text otherwise | TextboxField |
| any other data_type, or none | Field |
- Check the runtime type with a pattern such as field is ReferenceField reference before you read subclass properties.
Known issue When you read a schema with client.SerializerOptions or OpenTResponse<T>(), fields nested inside a GroupField or a modular block come back as plain Field objects without their subclass properties, such as format, reference_to, enum, min, and max. Sending that schema back with Update drops those properties from the nested fields. Edit the response as a JsonObject and send it back as JSON, or rebuild each nested field as its typed subclass before you call Update.
Method Index
Field has no methods. Create a subclass, set its properties, and add it to ContentModelling.Schema.
Class-Level Snippet
The snippet below fetches a content type and prints the subclass that the SDK created for each field of its schema.
using System;
using System.Text.Json;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").ContentType("<CONTENT_TYPE_UID>").Fetch();
// client.SerializerOptions picks the Field subclass for each schema entry.
ContentModelling? model = response.OpenJsonObjectResponse()["content_type"]?.Deserialize<ContentModelling>(client.SerializerOptions);
foreach (Field field in model?.Schema ?? new())
{
string details = field is ReferenceField reference ? $" -> {reference.ReferenceTo}" : "";
Console.WriteLine($"{field.Uid} ({field.DataType}): {field.GetType().Name}{details}");
}
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}