GroupField

View as Markdown

GroupField

GroupField describes a group field in a content type or global field schema, with data_type set to group. It holds a nested schema of other fields, and can repeat as a set.

When to Use This Class: Use this class to keep related fields together. For example, a Social Links group holds a platform and a URL, and repeats once for each link.

Conceptual Role: Acts as the Field subclass that the SDK creates for a field with data_type set to group.

Returns: GroupField. A group field of a schema.

Properties

GroupField inherits every property of Field and adds the properties below.

NameTypeRequiredDefaultDescription
Formatstring?NonullFormat of the group. Serialized as format.
SchemaList<Field>?NonullThe fields inside the group. Serialized as schema.
MaxInstanceint?NonullHighest number of times the group repeats. Serialized as max_instance.

Class-Level Notes

Nested fields.

  • Add any Field subclass to Schema. The SDK sends each nested field with the properties of its subclass.
  • Set Multiple to true to let an entry repeat the group, and MaxInstance to limit the number of repeats.

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

GroupField has no methods. Set its properties and add it to ContentModelling.Schema.

Class-Level Snippet

The snippet below creates a content type with a repeating group of social links.

using System;
using System.Collections.Generic;
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>");

ContentModelling model = new ContentModelling
{
    Title = "Blog Post",
    Uid = "blog_post",
    Schema = new List<Field>
    {
        new TextboxField { DisplayName = "Title", Uid = "title", DataType = "text", Mandatory = true, Unique = true },
        new GroupField
        {
            DisplayName = "Social Links",
            Uid = "social_links",
            DataType = "group",
            Multiple = true,
            MaxInstance = 5,
            Schema = new List<Field>
            {
                new TextboxField { DisplayName = "Platform", Uid = "platform", DataType = "text" },
                new TextboxField { DisplayName = "URL", Uid = "url", DataType = "text" }
            }
        }
    }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").ContentType().Create(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}