ImageField

View as Markdown

ImageField

ImageField describes a file field that accepts only images, with data_type set to file. It derives from FileField and adds dimension limits and image-specific field metadata.

When to Use This Class: Use this class for a field such as a banner image or an author photo, where the editor must pick an image.

Conceptual Role: Acts as the Field subclass that the SDK creates for a file field that has dimension, or field_metadata.image set to true.

Returns: ImageField. An image field of a schema.

Properties

ImageField inherits every property of FileField and Field, and adds or replaces the properties below.

NameTypeRequiredDefaultDescription
DimensionsDimension?NonullHeight and width limits of the image. Serialized as dimension.
FieldMetadataFileFieldMetadata?NonullField metadata with the image flag. Replaces Field.FieldMetadata. Serialized as field_metadata.

Dimension properties. Dimension holds the size limits of the image. It is in the Contentstack.Management.Core.Models.Fields namespace.

NameTypeRequiredDefaultDescription
HeightDictionary<string, int?>?NonullHeight limits keyed by min and max. Serialized as height.
WidthDictionary<string, int?>?NonullWidth limits keyed by min and max. Serialized as width.

Class-Level Notes

Field metadata.

  • ImageField declares its own FieldMetadata property of type FileFieldMetadata, which hides Field.FieldMetadata. Set and read it through a variable of type ImageField.
  • Through a variable of type Field, FieldMetadata is the base property. On an ImageField read from a response, that base property is null, and the metadata sits in the ImageField property.
  • Set FieldMetadata.AllowOnlyImage to true to send "image": true.

Method Index

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

Class-Level Snippet

The snippet below creates a content type with a banner image field that accepts images at least 600 pixels wide.

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 ImageField
        {
            DisplayName = "Banner",
            Uid = "banner",
            DataType = "file",
            Extensions = new List<string> { "jpg", "png" },
            Dimensions = new Dimension { Width = new Dictionary<string, int?> { ["min"] = 600 } },
            FieldMetadata = new FileFieldMetadata { AllowOnlyImage = true, Description = "Shown at the top of the post" }
        }
    }
};

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