Condition

View as Markdown

Condition

Condition describes one condition of a field visibility rule: the field to check, the operator, and the value to compare with.

When to Use This Class: Use this class to fill the conditions list of a FieldRules object.

Conceptual Role: Acts as the data model of one item in the conditions list of a field visibility rule.

Returns: Condition. One condition of a field visibility rule.

Properties

NameTypeRequiredDefaultDescription
DataTypestring?NonullData type of the operand field. Serialized as dataType.
OperandFieldstring?NonullUID of the field the condition checks. Serialized as operand_field.
Operatorstring?NonullComparison operator, for example equals. Serialized as operator.
Valuestring?NonullValue to compare the field with. Serialized as value.

Class-Level Notes

Serialization.

  • The JSON key of DataType uses camel case (dataType), unlike the other keys.
  • Value is a string. The SDK sends it as a JSON string, whatever the type of the operand field.
  • The Set Field Visibility Rule for Content Type request of the Content Management API reference describes the conditions list.

Method Index

Condition has no methods. Set its properties and add the object to FieldRules.conditions.

Class-Level Snippet

The snippet below updates a content type with a rule whose condition checks that the post type is video.

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;
using FieldAction = Contentstack.Management.Core.Models.Fields.Action;

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 TextboxField { DisplayName = "Post Type", Uid = "post_type", DataType = "text" },
        new TextboxField { DisplayName = "Video URL", Uid = "video_url", DataType = "text" }
    },
    FieldRules = new List<FieldRules>
    {
        new FieldRules
        {
            MatchType = "all",
            conditions = new List<Condition>
            {
                new Condition { OperandField = "post_type", Operator = "equals", Value = "video" }
            },
            Actions = new List<FieldAction>
            {
                new FieldAction { state = "show", TargetField = "video_url" }
            }
        }
    }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").ContentType("blog_post").Update(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}");
}