ContentType

View as Markdown

ContentType

A content type defines the structure of a page or a section of your app, and every entry follows the schema of one content type. Use this class to

  • read the schema of one content type
  • create an Entry object for one entry of the content type
  • create a Query object that searches the entries of the content type
  • add request headers that apply to this content type and the entries and queries you create from it

The contentType method on the stack returns the object and takes one argument:

NameTypeDescription
contentTypeNameStringUID of the content type.

To list every content type in the stack, use the getContentTypes method on the stack instead.

entry

entry creates an Entry object for one entry of this content type.

NameTypeDescription
entryUid (required)String

UID of the entry to work with.

Default: Not applicable

Returns: Entry. A new entry object.

Validation

  • There is no client-side validation. entry accepts an empty or null UID without an error.
  • An empty or null UID surfaces as an Error in the callback when the entry's fetch runs. Any other invalid UID surfaces when the API rejects that request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. entry builds the object and sends no request. Call fetch on the entry to read it.
  • The entry takes this object's headers when entry creates it, including the headers that you add with setHeader. A header you add to the content type afterwards does not reach this entry.
  • Each call returns a new object. Two calls with the same UID return two independent entries.
  • The SDK has one public entry method on ContentType, and it always takes a UID. To search entries, use the query method.

Example

Basic usage: create an entry object and fetch it

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

fetch

fetch reads the schema of this content type, without its entries.

NameTypeDescription
paramsJSONObject

Extra query parameters to send with the request.

Default: null
callback (required)ContentTypesCallback

Receives the content type or the error.

Default: Not applicable

Returns: void. The result arrives in the callback.

Validation

  • fetch checks that the content type UID is not empty or null. Without a UID, fetch sends no request and calls onCompletion with a null model and an Error whose status code and error code are 0. This callback runs on the calling thread, before fetch returns.
  • fetch accepts null for params and sends the request without extra parameters.
  • There is no validation of the keys or values in params. An invalid value surfaces as an Error in the callback when the API rejects the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • fetch calls Get a single content type (GET /v3/content_types/{content_type_uid}). One call sends one request.
  • onCompletion(ContentTypesModel contentTypesModel, Error error) fires when the request ends.
    • On success, error is null and contentTypesModel.getResponse() returns the content_type object from the response body as a JSONObject. The field definitions are in its schema array.
    • On this call contentTypesModel.getResultArray() returns an empty JSONArray. It holds content types only after the getContentTypes method on the stack.
    • On failure, contentTypesModel is null and error describes the failure.
  • fetch sends every key in params as a query parameter, and adds the environment query parameter from the stack. It writes environment into the JSONObject you pass.
  • fetch appends each parameter to the URL as key=value without URL encoding. Encode any value that holds a space or a reserved character such as &.
  • fetch always calls the network. It reads no cache and takes no cache policy.

Example

Basic usage: read the schema of one content type

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;

import org.json.JSONArray;
import org.json.JSONObject;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
    contentType.fetch(null, new ContentTypesCallback() {
        @Override
        public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
            if (error == null) {
                JSONObject contentTypeJson = contentTypesModel.getResponse();
                JSONArray fields = contentTypeJson.optJSONArray("schema");
                Log.d("Contentstack", contentTypeJson.optString("title") + ": "
                        + (fields == null ? 0 : fields.length()) + " fields");
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

All parameters: add the branch UID to the response with params

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;

import org.json.JSONObject;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");

    JSONObject params = new JSONObject();
    params.put("include_branch", true);

    contentType.fetch(params, new ContentTypesCallback() {
        @Override
        public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
            if (error != null) { Log.e("Contentstack", error.getErrorMessage()); return; }
            Log.d("Contentstack", contentTypesModel.getResponse().toString());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

query

query creates a Query object that searches the entries of this content type.

Returns: Query. A new query object.

Validation

  • There is no client-side validation in query.
  • An empty or null content type UID surfaces as an Error in the callback when find or findOne runs on the query. Any other invalid value surfaces when the API rejects that request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. query builds the object and sends no request. Add conditions on the query, then call find or findOne on it.
  • The query takes this object's headers when query creates it, including the headers that you add with setHeader. A header you add to the content type afterwards does not reach this query.
  • Each call returns a new query with no conditions.

Example

Basic usage: find the entries of a content type

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
    query.find(new QueryResultsCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Entries: " + queryResult.getResultObjects().size());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

removeHeader

removeHeader removes a header that setHeader added to this content type object.

NameTypeDescription
key (required)String

Name of the header to remove.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • removeHeader ignores the call when key is empty or null.
  • A key that this object does not hold changes nothing and raises no error.

Behavior

  • Client-side only. removeHeader changes this object and sends no request.
  • removeHeader removes only the headers that you add through setHeader on this object. A stack header with the same name, such as environment or branch, still goes out on this object's requests.
  • fetch stops sending the header on later calls. An entry or query you created from this object before the call keeps the header.

Example

Basic usage: stop sending a custom header

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
    contentType.setHeader("branch", "<BRANCH>");

    // Read the content type from the stack's branch instead
    contentType.removeHeader("branch");

    contentType.fetch(null, new ContentTypesCallback() {
        @Override
        public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
            if (error != null) { Log.e("Contentstack", error.getErrorMessage()); return; }
            Log.d("Contentstack", contentTypesModel.getResponse().optString("title"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

setHeader

setHeader adds a header to the requests of this content type object and of the entries and queries you create from it afterwards.

NameTypeDescription
key (required)String

Name of the header to send.

Default: Not applicable
value (required)String

Value of the header to send.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • setHeader ignores the call when key or value is empty or null. It records nothing and raises no error.
  • There is no other client-side validation.

Behavior

  • Client-side only. setHeader records the header on this object and sends no request.
  • A second call with the same key replaces the value.
  • The header replaces a stack header with the same name for this object's requests. The stack and other content type objects keep their own headers.
  • fetch sends the header on every later call.
  • entry and query take the header when they create an object. An entry or query you created before the call does not send it.
  • Use the removeHeader method to stop sending it.

Example

Basic usage: read a content type and its entries from a branch

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");

    // Set the header before you create the entry
    contentType.setHeader("branch", "<BRANCH>");

    Entry entry = contentType.entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) { Log.e("Contentstack", error.getErrorMessage()); return; }
            Log.d("Contentstack", entry.getTitle());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}