SelectEnum

View as Markdown

SelectEnum

SelectEnum holds the choices of a SelectField, and whether each choice is a single value or a key-value pair.

When to Use This Class: Use this class to set the Enum property of a select field.

Conceptual Role: Acts as the data model that a SelectField serializes under the enum key.

Returns: SelectEnum. The choices of a select field.

Properties

NameTypeRequiredDefaultDescription
AdvancedboolNofalsetrue for key-value choices, false for single values. Serialized as advanced.
ChoicesList<Dictionary<string, JsonElement>>?NonullThe choices, one dictionary each. Serialized as choices.

Class-Level Notes

Choice format.

  • With Advanced set to false, give each choice a value key, as the Create content type with select field request of the Content Management API reference shows. With Advanced set to true, give each choice a key and a value.
  • Each value is a JsonElement. Create one with JsonSerializer.SerializeToElement("News").
  • The SDK always sends advanced, with false when you leave it unset.

Method Index

SelectEnum has no methods. Set its properties and assign the object to SelectField.Enum.

Class-Level Snippet

The snippet below creates a content type with a select field whose choices are key-value pairs.

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

SelectEnum choices = new SelectEnum
{
    Advanced = true,
    Choices = new List<Dictionary<string, JsonElement>>
    {
        new() { ["key"] = JsonSerializer.SerializeToElement("News"), ["value"] = JsonSerializer.SerializeToElement("news") },
        new() { ["key"] = JsonSerializer.SerializeToElement("Tutorial"), ["value"] = JsonSerializer.SerializeToElement("tutorial") }
    }
};

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 = choices }
    }
};

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