Global Field Query

View as Markdown

Global Field Query

A global field is a reusable field, or group of fields, that you define once and reuse across content types in a stack. Use the GlobalFieldQuery class to list every global field in the stack in one request.

client.GlobalFieldQuery() on ContentstackClient returns a GlobalFieldQuery. It takes no arguments. To read one global field by its UID, use GlobalField instead.

Find

Find lists every global field in the stack.

NameTypeDescription
paramDictionary<string, object>

Extra query parameters to send with the request.

Default: null

Returns: Task<JsonObject>. The response body as a JSON object.

Validation

  • Throws ArgumentException before the request if param holds a key that the instance already sends, because the SDK adds each query parameter once. The instance always sends environment, and sends include_branch or include_global_field_schema after the matching modifier.
  • Throws ContentstackException if the request fails. Unlike GlobalField, this class throws the base type, not GlobalFieldException.

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

Behavior

  • Calls GET /global_fields (Get all global fields) and makes one HTTP request per call.
  • Returns the whole response body as a JsonObject. Read the global fields from its global_fields array.
  • Sends the environment, the values from IncludeBranch and IncludeGlobalFieldSchema, and every entry of param.
  • param applies to this call only. The SDK does not store it on the instance.

Example

Basic usage: list the global fields

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 body = await client.GlobalFieldQuery().Find();
    JsonArray globalFields = body["global_fields"]?.AsArray() ?? new JsonArray();
    foreach (JsonNode globalField in globalFields)
    {
        Console.WriteLine($"{globalField?["uid"]}: {globalField?["title"]}");
    }
}
catch (ContentstackException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}

All parameters: send extra query parameters

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

try
{
    Dictionary<string, object> param = new Dictionary<string, object>
    {
        { "include_global_field_schema", "true" },
        { "include_branch", "true" }
    };

    JsonObject body = await client.GlobalFieldQuery().Find(param);
    Console.WriteLine(body["global_fields"]?.AsArray().Count);
}
catch (ContentstackException e)
{
    Console.WriteLine(e.Message);
}

IncludeBranch

IncludeBranch adds the _branch key, which names the branch that holds the global fields, to the response.

Returns: GlobalFieldQuery. The same global field query, for chaining.

Validation

  • Throws ArgumentException if the instance already holds include_branch, because the SDK adds each query parameter once. This happens when you call IncludeBranch twice on one instance.
  • Find also throws ArgumentException if its param dictionary repeats include_branch.
  • An invalid value surfaces as a ContentstackException when Find sends the request.

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

Behavior

  • Client-side only. IncludeBranch stores include_branch and makes no request.
  • Find sends the value, and the API adds the _branch top-level key to the response (Get all global fields).
  • Does not choose the branch. The Branch option on ContentstackOptions sets the branch the SDK reads from.

Example

Basic usage: list the global fields with branch details

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 body = await client.GlobalFieldQuery()
        .IncludeBranch()
        .Find();
    Console.WriteLine(body.ToJsonString());
}
catch (ContentstackException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}

IncludeGlobalFieldSchema

IncludeGlobalFieldSchema adds the schema of each global field to the response.

Returns: GlobalFieldQuery. The same global field query, for chaining.

Validation

  • Throws ArgumentException if the instance already holds include_global_field_schema, because the SDK adds each query parameter once. This happens when you call IncludeGlobalFieldSchema twice on one instance.
  • Find also throws ArgumentException if its param dictionary repeats include_global_field_schema.
  • An invalid value surfaces as a ContentstackException when Find sends the request.

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

Behavior

  • Client-side only. IncludeGlobalFieldSchema stores include_global_field_schema and makes no request.
  • Find sends the value, and the API adds the schema of each global field to the response (Get all global fields).

Example

Basic usage: list the global fields with their schemas

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 body = await client.GlobalFieldQuery()
        .IncludeGlobalFieldSchema()
        .Find();
    foreach (JsonNode globalField in body["global_fields"]?.AsArray() ?? new JsonArray())
    {
        JsonArray schema = globalField?["schema"]?.AsArray() ?? new JsonArray();
        Console.WriteLine($"{globalField?["uid"]} has {schema.Count} fields");
    }
}
catch (ContentstackException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.Message}");
}