ContentType
ContentType
ContentType represents one content type of a stack. Use this class to
- fetch the schema of the content type
- create an Entry object to fetch one entry
- create a Query object to fetch a list of entries
The class has no public constructor. Create an instance with the ContentType method of ContentstackClient, which takes the parameter below:
| Name | Type | Description |
|---|---|---|
| contentTypeName | string | UID of the content type. |
Entry
Entry creates an entry object for one entry of this content type.
| Name | Type | Description |
|---|---|---|
| entryUid (required) | string | UID of the entry to fetch. |
Returns: Entry. A new entry object.
Validation
- There is no client-side validation. Entry accepts an empty string or null and raises no error.
- With an empty or null UID, the Fetch call that follows throws EntryException, because the SDK then requests the entry list path instead of one entry.
- The API rejects an unknown UID, and Fetch throws EntryException when it sends the request.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. Entry makes no request. Call Fetch on the object it returns.
- Each call returns a new object, so options chained on one entry object do not affect another.
- The new object takes the headers of this content type at the time of the call.
- A non-empty UID also becomes the client's current entry UID, which the LivePreviewQueryAsync method uses when its query names no entry.
- See the Entry class for every option and getter.
Example
Basic usage: create an entry object and fetch it
using System;
using Contentstack.Core;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Entry request = client.ContentType("<CONTENT_TYPE_UID>").Entry("<ENTRY_UID>");
try
{
Entry entry = await request.Fetch<Entry>();
Console.WriteLine(entry.GetTitle());
}
catch (ContentstackException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}Fetch
Fetch retrieves the schema of the content type from the Content Delivery API.
| Name | Type | Description |
|---|---|---|
| param | Dictionary<string, object> | Extra query parameters for the request. |
Returns: Task<JsonObject>. The requested content type.
Validation
- There is no client-side validation of the content type UID or of param.
- Throws ContentTypeException, a subclass of ContentstackException, when the API rejects the request, for example for an unknown content type UID or an invalid delivery token. Message, ErrorCode, StatusCode, and Errors carry the error details from the API.
- Throws ContentTypeException when the response holds no content_type object. An empty content type UID causes this.
- Throws ContentTypeException if param holds the key environment, because the SDK already sends the client's environment under that name.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Fetch sends one GET /content_types/{content_type_uid} request with the client's environment and each param entry as a query parameter. See Get a single content type in the Content Delivery API reference.
- It returns the content_type object of the response, not the whole response body. Read the schema from its schema key.
- The SDK writes each param value with ToString(), so true goes out as True. Pass the string "true" for a flag.
- Useful parameters include include_branch, which adds the _branch key, and include_global_field_schema, which adds the schema of each Global field to the content type schema. See the tip in Get all content types.
- Each call sends a new request. The SDK does not cache the schema.
Example
Basic usage: fetch a content type schema
using System;
using System.Text.Json.Nodes;
using Contentstack.Core;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
try
{
JsonObject contentType = await client.ContentType("<CONTENT_TYPE_UID>").Fetch();
Console.WriteLine(contentType["title"]);
}
catch (ContentstackException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}All parameters: include Global field schemas and the branch
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Contentstack.Core;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Dictionary<string, object> param = new Dictionary<string, object>
{
{ "include_global_field_schema", "true" },
{ "include_branch", "true" }
};
try
{
JsonObject contentType = await client.ContentType("<CONTENT_TYPE_UID>").Fetch(param);
Console.WriteLine(contentType["_branch"]);
Console.WriteLine(contentType["schema"]?.AsArray().Count);
}
catch (ContentstackException e)
{
Console.WriteLine(e.Message);
}Query
Query creates a query object across the entries of this content type.
Returns: Query. A new query object.
Validation
- There is no client-side validation.
- An unknown content type UID fails when the query sends its request: Find throws QueryFilterException.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. Query makes no request. Chain filters on the object it returns, then call Find or FindOne.
- Each call returns a new object, so filters on one query do not affect another.
- The new object takes the headers of this content type at the time of the call.
- See the Query class for every filter and option.
Example
Basic usage: list the first ten entries
using System;
using Contentstack.Core;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
try
{
Query query = client.ContentType("<CONTENT_TYPE_UID>").Query();
ContentstackCollection<Entry> entries = await query.Limit(10).Find<Entry>();
foreach (Entry entry in entries)
Console.WriteLine(entry.GetTitle());
}
catch (ContentstackException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}RemoveHeader
RemoveHeader deletes a header from the headers that SetHeader writes on this content type.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Name of the header to remove. |
Returns: void. This method returns nothing.
Validation
- Throws ArgumentNullException if key is null.
- A name that the content type does not hold leaves it unchanged and raises no error.
Behavior
- Client-side only. RemoveHeader makes no request.
- It removes only headers set with SetHeader on this content type. Client headers, such as api_key and access_token, stay in place.
- Entry and Query objects created before the call keep the header. Objects created after it do not have it.
Example
Basic usage: remove a header before creating an entry
using System;
using Contentstack.Core;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = client.ContentType("<CONTENT_TYPE_UID>");
contentType.SetHeader("branch", "<BRANCH>");
contentType.RemoveHeader("branch");
try
{
// This entry has no content type branch header.
Entry entry = await contentType.Entry("<ENTRY_UID>").Fetch<Entry>();
Console.WriteLine(entry.GetTitle());
}
catch (ContentstackException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}SetHeader
SetHeader adds a request header for this content type and the entries and queries you create from it afterwards.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Name of the header. |
| value (required) | string | Value of the header. |
Returns: void. This method returns nothing.
Validation
- There is no client-side validation of the header name or value.
- SetHeader ignores the call when key or value is null, and raises no error.
- A request skips, without an error, a header that .NET does not let a request set directly, such as Content-Type or User-Agent.
Behavior
- Client-side only. SetHeader stores the header and makes no request.
- The header goes out with Fetch on this content type, and with every Entry or Query object you create from it after the call. Objects created earlier keep the headers they had.
- A second call with the same key replaces the value.
- A content type header replaces a client header of the same name. A header set on an entry or a query replaces it in turn.
Example
Basic usage: send a branch header for one content type
using System;
using Contentstack.Core;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = client.ContentType("<CONTENT_TYPE_UID>");
contentType.SetHeader("branch", "<BRANCH>");
try
{
// Create the entry after SetHeader so that it takes the header.
Entry entry = await contentType.Entry("<ENTRY_UID>").Fetch<Entry>();
Console.WriteLine(entry.GetTitle());
}
catch (ContentstackException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}