TermModel

View as Markdown

TermModel

TermModel holds the details of a taxonomy term. To create a term with the Create method, set Uid, Name, and optionally ParentUid. The API fills the depth, count, hierarchy, and date fields when it returns a term.

When to Use This Class: Use this class to build the request body for a new, updated, or localized term. Use it to read a fetched term as a typed object.

Conceptual Role: Acts as the data model that the Term class serializes under the term key of a request.

Returns: TermModel. The details of a taxonomy term.

Properties

NameTypeRequiredDefaultDescription
Uidstring?NonullTerm UID. Set it to create a term. Serialized as uid.
Namestring?NonullDisplay name of the term. Set it to create a term. Serialized as name.
TaxonomyUidstring?NonullUID of the taxonomy that holds the term. The API sets it. Serialized as taxonomy_uid.
ParentUidstring?NonullUID of the parent term. null places the term at the root level. Serialized as parent_uid.
Depthint?NonullLevel of the term in the hierarchy. The API sets it. Serialized as depth.
ChildrenCountint?NonullNumber of child terms. The API sets it. Serialized as children_count.
ReferencedEntriesCountint?NonullNumber of entries that carry the term. The API sets it. Serialized as referenced_entries_count.
AncestorsList<TermAncestorDescendant>?NonullTerms above this term. The API sets it. Serialized as ancestors.
DescendantsList<TermAncestorDescendant>?NonullTerms below this term. The API sets it. Serialized as descendants.
CreatedAtstring?NonullCreation time as an ISO 8601 string. The API sets it. Serialized as created_at.
UpdatedAtstring?NonullLast update time as an ISO 8601 string. The API sets it. Serialized as updated_at.

The Ancestors and Descendants lists hold TermAncestorDescendant objects.

Class-Level Notes

Fields you send and fields the API sets.

  • The SDK serializes every property that is not null and leaves out the null ones. A model with only Uid and Name set produces a body with only uid and name, which creates a root-level term.
  • Leave TaxonomyUid, the counts, Depth, Ancestors, Descendants, and the dates unset in a request. The API sets them, and the API reference documents no effect for them in a request.

Position is not part of the model.

  • TermModel has no order property, so Create cannot set the position of a term among its siblings. Call Move with a TermMoveModel after you create the term.

Counts appear only on request.

  • The API returns ChildrenCount and ReferencedEntriesCount only when you pass include_children_count or include_referenced_entries_count. The properties stay null otherwise.

Dates are strings.

  • CreatedAt and UpdatedAt hold the timestamp string the API returns. Parse them with DateTimeOffset.Parse when you need a date value.

Method Index

TermModel has no methods. Set and read its properties directly, and pass the model to the Create, Update, or Localize method.

Class-Level Snippet

The snippet below creates a child term, then reads it back into a second model.

using System;
using System.Text.Json;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
Taxonomy taxonomy = client.Stack("<API_KEY>").Taxonomy("<TAXONOMY_UID>");

try
{
    TermModel model = new TermModel
    {
        Uid = "running_shoes",
        Name = "Running Shoes",
        ParentUid = "footwear"
    };
    taxonomy.Terms().Create(model);

    ContentstackResponse response = taxonomy.Terms("running_shoes").Fetch();
    TermModel? term = response.OpenJsonObjectResponse()["term"]?.Deserialize<TermModel>();
    Console.WriteLine($"{term?.Name} sits at depth {term?.Depth} in {term?.TaxonomyUid}");
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects a request, for example an unknown parent term
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}