ContentModelling

View as Markdown

ContentModelling

ContentModelling holds the schema of a content type or a global field: its title, UID, fields, field visibility rules, and page options. Pass it to the Create and Update methods of ContentType and GlobalField.

When to Use This Class: Use this class to describe a content type or global field that you create or update. It also reads a fetched schema as typed objects.

Conceptual Role: Acts as the data model that ContentType serializes under the content_type key, and GlobalField under the global_field key, of a request body.

Returns: ContentModelling. The schema of a content type or global field.

Properties

NameTypeRequiredDefaultDescription
Titlestring?NonullDisplay name of the content type or global field. Serialized as title.
Uidstring?NonullUnique ID of the content type or global field. Serialized as uid.
Descriptionstring?NonullDescription shown in the stack. Serialized as description.
SchemaList<Field>?NonullThe fields, in display order. Each item is a Field subclass. Serialized as schema.
FieldRulesList<FieldRules>?NonullField visibility rules. See FieldRules. Serialized as field_rules.
GlobalFieldRefsList<GlobalFieldRefs>?NonullNested global fields that a global field schema uses. See GlobalFieldRefs. Serialized as global_field_refs.
OptionsOption?NonullPage settings of a content type. See Option. Serialized as options.

Class-Level Notes

Fields the SDK sends.

  • The SDK leaves out every property that is null, so the request body holds only the properties you set.
  • The SDK checks none of the properties. The API validates the request body. The Create a content type and Create a global field requests of the Content Management API reference list the properties each request needs.

Reading a fetched schema.

  • Deserialize the content_type or global_field node of a response with client.SerializerOptions. Those options pick the Field subclass for each top-level schema entry by its data_type, as the Field page describes.
  • Deserializing with default JsonSerializerOptions returns every schema entry as a plain Field, without the properties of the subclass.

Method Index

ContentModelling has no methods. Set and read its properties directly, and pass the object to the Create or Update method.

Class-Level Snippet

The snippet below builds a content type schema with a title field, a select field, and a page URL, then creates the content type.

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",
    Description = "Articles for the company blog",
    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") }
                }
            }
        }
    },
    Options = new Option { Title = "title", IsPage = true, UrlPrefix = "/blog/" }
};

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