SelectField

View as Markdown

SelectField

SelectField describes a select field in a content type or global field schema: a text field whose value comes from a fixed list of choices. It adds the list of choices to the properties of Field.

When to Use This Class: Use this class for a dropdown or a set of checkboxes, for example a Category field with the choices News and Tutorial.

Conceptual Role: Acts as the Field subclass that the SDK creates for a text field that has enum, or a display_type of dropdown or checkbox.

Returns: SelectField. A select field of a schema.

Properties

SelectField inherits every property of Field and adds the property below.

NameTypeRequiredDefaultDescription
EnumSelectEnum?NonullThe choices and their format. Serialized as enum.

Class-Level Notes

Field settings.

  • Set DataType to text and DisplayType to dropdown or checkbox. Set Multiple to true to let an entry hold more than one choice.
  • The Create content type with select field request of the Content Management API reference describes the enum object.

Method Index

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

Class-Level Snippet

The snippet below creates a content type with a dropdown field that offers two categories.

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

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 SelectField
        {
            DisplayName = "Category",
            Uid = "category",
            DataType = "text",
            DisplayType = "dropdown",
            Enum = new SelectEnum
            {
                Advanced = false,
                Choices = new List<Dictionary<string, JsonElement>>
                {
                    new() { ["value"] = JsonSerializer.SerializeToElement("News") },
                    new() { ["value"] = JsonSerializer.SerializeToElement("Tutorial") }
                }
            }
        }
    }
};

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}");
}