GlobalFields

View as Markdown

GlobalFields

A Global field is a reusable field (or group of fields) that you can define once and reuse in any content type within your stack. This eliminates the need (and thereby time and effort) to repeatedly create the same set of fields in multiple content types.

You can now pass the branch header in the API request to fetch or manage modules located within specific branches of the stack. You can also set the include_branch query parameter to true to include the _branch top-level key in the response. This key specifies the unique ID of the branch where the concerned Contentstack module resides.

A nested global field is a reusable set of sub-fields you can embed within other global fields or content types. It helps manage complex data structures more efficiently across your stack.

Note To work with nested global fields, set api_version to 3.2 in the request headers. This version is required for all CRUD operations to work correctly.

create

The Create a global field request allows you to create a new global field in a particular stack of your Contentstack account. You can use this global field in any content type within your stack.

NoteOnly the stack owner, administrator, and developer can create global fields.
You need to use either the stack's Management Token or the user Authtoken (one of them is mandatory), along with the stack API key, to make a valid Content Management API request. Read more about authentication.

NameTypeDescription
body (required)JSONObject

The request body.

Example 1:

import contentstack;
Contentstack contentstack = new Contentstack.Builder().build();
GlobalField globalField = contentstack.stack().globalField();
Call<ResponseBody> response = globalField.create(body).execute();
if(response.isSuccessful) {
    System.out.println("Response" + response);
}

Example 2:

// For Nested global fields pass api_version param with value 3.2
import com.contentstack.cms.Contentstack;
Contentstack contentstack = new Contentstack.Builder().build();
GlobalField globalField = contentstack.stack().globalField();
globalField.addHeader("api_version","3.2");
Response<ResponseBody> response = globalField.create(body).execute();
if(response.isSuccessful) {
    System.out.println("Response" + response);
} else {
    System.out.println("Error: "+ response.errorBody().string());
}

imports

The "Import global field" call imports global fields into Stack.

NoteYou need to use either the stack's Management Token or the user Authtoken (one of them is mandatory), along with the stack API key, to make a valid Content Management API request.

NameTypeDescription
body (required)Object

The request body.

import contentstack;

Contentstack contentstack = new Contentstack.Builder().build();

GlobalField globalField = contentstack.stack().globalField();

Call<ResponseBody> response = globalField.imports(body).execute();

if(response.isSuccessful) {

    System.out.println("Response" + response);

}

export

This request is used to export a specific global field and its schema. The data is exported in JSON format.

NameTypeDescription
versionInteger

The version of the content type you want to retrieve. If the version number is not specified, you will get the latest version of the content type.

import contentstack;

Contentstack contentstack = new Contentstack.Builder().build();

GlobalField globalField = contentstack.stack().globalField();

Call<ResponseBody> response = globalField.export().execute();

if(response.isSuccessful) {

    System.out.println("Response" + response);

}

update

The "Update a global field" request allows you to update the schema of an existing global field.

When executing the API call, in the URI Parameters section, provide the unique ID of your global field.

NoteYou need to use either the stack's Management Token or the user Authtoken (one of them is mandatory), along with the stack API key, to make a valid Content Management API request.

NameTypeDescription
requestBody (required)JSONObject

The request body.

import contentstack;

Contentstack contentstack = new Contentstack.Builder().build();

GlobalField globalField = contentstack.stack().globalField();

Call<ResponseBody> response = globalField.update(requestBody).execute();

if(response.isSuccessful) {

    System.out.println("Response" + response);

}

removeParam

Set header for the request.

NameTypeDescription
key (required)String

Removes query param using key of request.

import contentstack;

Contentstack contentstack = new Contentstack.Builder().build();

GlobalField globalField = contentstack.stack().globalField();

globalField.removeParam("key");

find

The "Get all global fields" call returns comprehensive information on all the global fields available in a particular stack in your account

NoteYou need to use either the stack's Management Token or the user Authtoken (one of them is mandatory), along with the stack API key, to make a valid Content Management API request. Read more about authentication.

import contentstack;

Contentstack contentstack = new Contentstack.Builder().build();

GlobalField globalField = contentstack.stack().globalField();

Call<ResponseBody> response = globalField.find().execute();

if(response.isSuccessful) {

    System.out.println("Response" + response);

}

fetch

The "Get a Single global field" request lets you fetch comprehensive details of a specific global field.

When executing the API call, in the URI Parameters section, provide the unique ID of your global field.

NoteYou need to use either the stack's Management Token or the user Authtoken (one of them is mandatory), along with the stack API key, to make a valid Content Management API request.

import contentstack;

Contentstack contentstack = new Contentstack.Builder().build();

GlobalField globalField = contentstack.stack().globalField();

Call<ResponseBody> response = globalField.fetch().execute();

if(response.isSuccessful) {

    System.out.println("Response" + response);

}