TaxonomyField

View as Markdown

TaxonomyField

TaxonomyField describes a taxonomy field in a content type schema, with data_type set to taxonomy. It lists the taxonomies whose terms an entry can hold, with limits for each one.

When to Use This Class: Use this class to let editors tag entries with terms, for example a Topics taxonomy on a blog post.

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

Returns: TaxonomyField. A taxonomy field of a schema.

Properties

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

NameTypeRequiredDefaultDescription
TaxonomiesList<TaxonomyFieldBinding>?NonullThe taxonomies the field uses, with limits. Serialized as taxonomies.

TaxonomyFieldBinding properties. TaxonomyFieldBinding links the field to one taxonomy. It is in the Contentstack.Management.Core.Models.Fields namespace.

NameTypeRequiredDefaultDescription
TaxonomyUidstring?NonullUID of the taxonomy. Serialized as taxonomy_uid.
MaxTermsint?NonullHighest number of terms an entry can hold from this taxonomy. Serialized as max_terms.
MandatoryboolNofalseRequires at least one term from this taxonomy. Serialized as mandatory.
MultipleboolNofalseLets an entry hold more than one term from this taxonomy. Serialized as multiple.
NonLocalizableboolNofalseKeeps the terms the same in every locale. Serialized as non_localizable.

Class-Level Notes

Field settings.

  • The Create content type with taxonomy request of the Content Management API reference shows the taxonomies list, with multiple set to true on the field.
  • Set DataType to taxonomy. The SDK reads a fetched field as TaxonomyField only when its data_type is taxonomy.
  • The SDK always sends mandatory, multiple, and non_localizable for each binding, with false when you leave them unset. It leaves out max_terms when it is null.

Method Index

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

Class-Level Snippet

The snippet below creates a content type with a taxonomy field that accepts up to five terms from a topics taxonomy.

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 TaxonomyField
        {
            DisplayName = "Taxonomies",
            Uid = "taxonomies",
            DataType = "taxonomy",
            Multiple = true,
            Taxonomies = new List<TaxonomyFieldBinding>
            {
                new TaxonomyFieldBinding { TaxonomyUid = "topics", MaxTerms = 5, Mandatory = true, Multiple = true }
            }
        }
    }
};

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