Field

View as Markdown

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

NameTypeRequiredDefaultDescription
DisplayNamestring?NonullName of the field in the entry editor. The API requires it. Serialized as display_name.
Uidstring?NonullUnique ID of the field within the schema. The API requires it. Serialized as uid.
DataTypestring?NonullData type of the field, for example text, number, or reference. Serialized as data_type.
FieldMetadataFieldMetadata?NonullExtra settings such as a default value or instructions. Serialized as field_metadata.
MultipleboolNofalseLets the field hold more than one value. Serialized as multiple.
MandatoryboolNofalseRequires a value in every entry. Serialized as mandatory.
UniqueboolNofalseRequires a value that no other entry uses. Serialized as unique.
DisplayTypestring?NonullPresentation 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 fieldClass the SDK creates
extension_uid has a valueExtensionField
data_type is groupGroupField
data_type is blocksModularBlockField
data_type is referenceReferenceField
data_type is global_fieldGlobalFieldReference
data_type is extensionExtensionField
data_type is taxonomyTaxonomyField
data_type is numberNumberField
data_type is isodateDateField
data_type is file, and the field has dimension or field_metadata.image set to trueImageField
data_type is file otherwiseFileField
data_type is jsonJsonField
data_type is text, and the field has enum or display_type set to dropdown or checkboxSelectField
data_type is text otherwiseTextboxField
any other data_type, or noneField
  • 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}");
}