CustomFieldModel
CustomFieldModel
CustomFieldModel holds the HTML file, title, data type, and settings of a custom field that you upload with the Upload method of Extension. Contentstack then hosts the file.
When to Use This Class: Use this class when the code of your custom field is an HTML file on disk or in memory. For a custom field that another server hosts, use ExtensionModel with the Create method instead.
Conceptual Role: Implements IExtensionInterface, the interface that Extension.Upload takes. It builds the multipart/form-data body of the upload request and sets extension[type] to field.
Returns: CustomFieldModel. The file and settings of a custom field upload.
Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| Title | string | Yes | The title constructor argument | Name of the custom field. Sent as extension[title], and as the file name of the upload. |
| DataType | string | No | The dataType constructor argument | Data type the field stores, for example text. Sent as extension[data_type] when it is not null. |
| Tags | string? | No | The tags constructor argument, or null if you omit it | Tags for the custom field. Sent as extension[tags] when it is not null. |
| Multiple | bool | No | The isMultiple constructor argument, or false if you omit it | Whether the field stores multiple values. Always sent as extension[multiple]. |
| ContentType | string | Yes | The contentType constructor argument | MIME type of the uploaded file, for example text/html. |
Class-Level Notes
How the SDK builds the upload body.
- The CustomFieldModel constructor reads the whole file into memory and sets the MIME type of the file part from contentType.
- Extension.Upload calls GetHttpContent, which reads Title, DataType, Tags, and Multiple at that moment. A change to those properties after construction reaches the request. A change to ContentType does not change the MIME type of the file part.
- The Upload a custom field request of the Content Management API reference describes the form fields.
The interface it implements.
- IExtensionInterface extends IUploadInterface. It requires Title, Tags, ContentType, and GetHttpContent(). Implement it yourself only when you need a form field that the three SDK models do not send.
Method Index
| Method Name | Returns | Description |
|---|---|---|
| CustomFieldModel | A new custom field upload model. | Creates the model from a file path, stream, byte array, or ByteArrayContent. |
| GetHttpContent | The multipart form body of the upload. | Builds the request body that Upload sends. |
Class-Level Snippet
The snippet below builds a custom field from an HTML file and uploads it to the stack.
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.CustomExtension;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
CustomFieldModel model = new CustomFieldModel("<FILE_PATH>", "text/html", "Color Picker", "text", false, "design");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").Extension().Upload(model);
Console.WriteLine(response.OpenJsonObjectResponse()["extension"]?["uid"]);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}Methods
Each method of the CustomFieldModel class has its own section below, with its parameters, validation rules, behavior, and examples.
CustomFieldModel
CustomFieldModel creates a custom field upload model from an HTML file, a stream, a byte array, or a ByteArrayContent.
| Name | Type | Description |
|---|---|---|
| filePath (required) | string | Path of the HTML file to upload. |
| contentType (required) | string | MIME type of the file, for example text/html. |
| title (required) | string | Name of the custom field. |
| dataType (required) | string | Data type the field stores, for example text. |
| isMultiple | bool | Whether the field stores multiple values. |
| tags | string? | Tags for the custom field. |
| stream (required) | Stream | Stream that holds the HTML file. |
| bytes (required) | byte[] | Content of the HTML file. |
| byteArray (required) | ByteArrayContent | HTTP content that holds the HTML file. |
Returns: CustomFieldModel. A new custom field upload model.
Overloads
- CustomFieldModel(string filePath, string contentType, string title, string dataType, bool isMultiple = false, string? tags = null)
- CustomFieldModel(Stream stream, string contentType, string title, string dataType, bool isMultiple = false, string? tags = null)
- CustomFieldModel(byte[] bytes, string contentType, string title, string dataType, bool isMultiple = false, string? tags = null)
- CustomFieldModel(ByteArrayContent byteArray, string contentType, string title, string dataType, bool isMultiple = false, string? tags = null)
Validation
- Throws ArgumentNullException (parameter title) if title is null, because the API needs a title for the extension.
- Throws ArgumentNullException (parameter byteArray) if byteArray is null.
- Throws ArgumentNullException (parameter mediaType) if contentType is null, because the SDK sets it as the MIME type of the file part.
- Throws FormatException if contentType is not a valid MIME type.
- The file path, stream, and byte array overloads read the file before they check title:
- filePath set to null throws ArgumentNullException (parameter path), and a missing file throws FileNotFoundException or DirectoryNotFoundException.
- stream set to null throws NullReferenceException. A stream that does not support Length, such as a network stream, throws NotSupportedException. Copy it into a MemoryStream first.
- bytes set to null throws ArgumentNullException (parameter content).
- dataType set to null raises no error. The SDK then leaves out the extension[data_type] form field.
- The constructor sends no request. Errors from the API come from Extension.Upload.
Behavior
- Reads the whole file into memory when you construct the model. The stream overload reads from the current position for stream.Length bytes.
- Sets the MIME type of the file part to contentType, and stores the other arguments in the properties of the same name. isMultiple sets Multiple.
- The file path overload opens the file with File.OpenRead and reads it. Use the byte array overload with File.ReadAllBytes when you want to control when the file closes.
Example
Basic usage: build a model from a file path
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.CustomExtension;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
CustomFieldModel model = new CustomFieldModel("<FILE_PATH>", "text/html", "Color Picker", "text");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").Extension().Upload(model);
Console.WriteLine(response.OpenJsonObjectResponse()["extension"]?["uid"]);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: build models from a stream, bytes, and ByteArrayContent
using System;
using System.IO;
using System.Net.Http;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.CustomExtension;
ContentstackClient client = new ContentstackClient();
byte[] html = File.ReadAllBytes("<FILE_PATH>");
using MemoryStream stream = new MemoryStream(html);
CustomFieldModel fromStream = new CustomFieldModel(stream, "text/html", "Color Picker", "text", false, "design");
CustomFieldModel fromBytes = new CustomFieldModel(html, "text/html", "Tag List", "text", true, "design");
CustomFieldModel fromContent = new CustomFieldModel(new ByteArrayContent(html), "text/html", "Star Rating", "number", false, null);
try
{
ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Extension().Upload(fromBytes);
Console.WriteLine(response.OpenJsonObjectResponse()["extension"]?["multiple"]);
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Edge case: a null title throws before any request
using System;
using Contentstack.Management.Core.Models.CustomExtension;
try
{
CustomFieldModel model = new CustomFieldModel(new byte[] { 60, 112, 62 }, "text/html", null!, "text");
}
catch (ArgumentNullException e)
{
// Fires in the constructor when title is null
Console.WriteLine(e.ParamName);
}GetHttpContent
GetHttpContent builds the multipart/form-data body that Extension.Upload sends for a custom field.
This method takes no parameters.
Returns: HttpContent. The multipart form body of the upload.
Validation
- Throws no exception of its own. The constructor already checked the file and the title.
- GetHttpContent sends no request. Extension.Upload calls it and sends the result.
Behavior
- Returns a new MultipartFormDataContent on each call, with the form fields listed below in this order:
- extension[upload]: the file, with Title as its file name
- extension[title]: Title
- extension[data_type]: DataType, only when it is not null
- extension[tags]: Tags, only when it is not null
- extension[multiple]: true or false
- extension[type]: field
- You do not need to call GetHttpContent yourself. Call it to inspect the form before an upload, for example in a unit test.
Example
Basic usage: inspect the form fields of a custom field upload
using System;
using System.Net.Http;
using Contentstack.Management.Core.Models.CustomExtension;
CustomFieldModel model = new CustomFieldModel(new byte[] { 60, 112, 62 }, "text/html", "Color Picker", "text", false, "design");
HttpContent content = model.GetHttpContent();
foreach (HttpContent part in (MultipartFormDataContent)content)
{
Console.WriteLine(part.Headers.ContentDisposition?.Name);
}