.NET Utils SDK Variant Utility

View as Markdown
Last updated July 17, 2026

The Variant Utility reads variant alias data from CDA entry responses and formats it for application use. Use this utility when working with variant-aware entries that include publish_details.variants.

It helps you:

  • Extract variant aliases per entry
  • Generate the data-csvariants attribute for frontend rendering

Use cases

Use this utility when:

  • You fetch entries with variant headers (for example, x-cs-variant-uid)
  • You need alias values for personalization or targeting
  • You want to render variant data in HTML using data-csvariants

Method index

MethodDescription
GetVariantAliases(JsonObject, string)Returns variant aliases for a single entry.
GetVariantAliases(JsonArray, string)Returns variant aliases for multiple entries.
GetVariantMetadataTags(JsonObject, string)Builds the HTML attribute payload for a single entry.
GetVariantMetadataTags(JsonArray, string)Builds the HTML attribute payload for multiple entries.

GetVariantAliases (Single Entry)

The GetVariantAliases(JsonObject, string) retrieves each variant’s alias from publish_details.variants on the CDA entry JSON and returns one result as a JsonObject.

Parameters

NameTypeRequiredDescription
entryJsonObjectYesCDA entry used to read publish_details.variants
contentTypeUidstringYesContent type UID for the entry. Returned as contenttype_uid in the result

Returns

ValueDescription
JsonObjectFor single-entry input. Contains entry_uid, contenttype_uid, and variants

Validation

Throws ArgumentException if:

  • entry is null
  • entry.uid is missing
  • contentTypeUid is null or empty

Behavior

  • If the payload contains a nested entry object, the method reads from that object.

Example

The following example retrieves variant aliases for a single entry:

// Initialize the utility and process a single entry
var variantUtils = new VariantUtility();
JsonObject entry = GetEntryFromJson(); // Method to fetch your CDA entry
string contentTypeUid = "product";

try {
    JsonObject result = variantUtils.GetVariantAliases(entry, contentTypeUid);
    // Access aliases from the result object
    var aliases = result["variants"];
} catch (ArgumentException ex) {
    Console.WriteLine($"Validation Error: {ex.Message}");
}

GetVariantAliases (Multiple Entries)

The Utils.GetVariantAliases(JsonArray, string) retrieves variant aliases for each CDA entry in a JsonArray and returns one result object per valid entry as a JsonArray.

Parameters

NameTypeRequiredDescription
entriesJsonArrayNoCDA entries to process. Returns an empty array if null. Skips entries with invalid UIDs.
contentTypeUidstringYesContent type UID applied to all entries. Returned as contenttype_uid in each result object.

Returns

ValueDescription
JsonArrayEach element has the same shape as the single-entry JsonObject result (entry_uid, contenttype_uid, variants).

Validation

  • Throws ArgumentException if contentTypeUid is null or empty.

Behavior

  • Skips invalid entries and processes only entries with a valid UID.

Example

The following example retrieves variant aliases for multiple entries:

// Initialize the utility and process multiple entries
var variantUtils = new VariantUtility();
JsonArray entries = GetEntriesFromListResponse(); // Method to fetch your CDA entries list
string contentTypeUid = "product";

try {
    JsonArray results = variantUtils.GetVariantAliases(entries, contentTypeUid);
    foreach (var result in results) {
        Console.WriteLine($"Entry: {result["entry_uid"]}, Aliases: {result["variants"]}");
    }
} catch (ArgumentException ex) {
    Console.WriteLine($"Validation Error: {ex.Message}");
}

GetVariantMetadataTags (Single Entry)

The GetVariantMetadataTags(JsonObject, string) method builds a JsonObject for HTML integration. The data-csvariants property is a string containing a serialized JSON array of variant details.

Parameters

NameTypeRequiredDescription
entryJsonObjectNoCDA entry. Defines the entry to serialize. Returns data-csvariants as "[]" if null.
contentTypeUidstringYes (if entry provided)Content type UID for the entry. Required whenever entry is not null.

Returns

ValueDescription
JsonObjectContains data-csvariants as a string (serialized JSON array). Each element represents { entry_uid, contenttype_uid, variants }.

Note The data-csvariants value is a serialized JSON array where each element follows the shape { entry_uid, contenttype_uid, variants }. Exact formatting depends on System.Text.Json serialization.

Validation

  • Throws ArgumentException if entry is provided, and contentTypeUid is null or empty.

Behavior

  • Processes a single entry via the array path (same pipeline as the JsonArray overload with one element).
  • Returns { "data-csvariants": "[]" } if entry is null. It does not throw an error.

Note The data-csvariants is returned as raw JSON text. In standard Razor or Blazor attribute binding, output is HTML-encoded automatically. If you inject this value into raw HTML or bypass framework encoding, encode it for an HTML attribute before rendering.

Example

The following example builds the attribute for a single entry:

// Initialize the utility and generate metadata tags
var variantUtils = new VariantUtility();
JsonObject entry = GetEntryFromJson();
string contentTypeUid = "product";

try {
    JsonObject attributeMap = variantUtils.GetVariantMetadataTags(entry, contentTypeUid);
    string jsonString = attributeMap["data-csvariants"].ToString();
    // Output: "[{\"entry_uid\":\"...\",\"contenttype_uid\":\"...\",\"variants\":[...]}]"
} catch (ArgumentException ex) {
    Console.WriteLine(ex.Message);
}

GetVariantMetadataTags (Multiple Entries)

The GetVariantMetadataTags(JsonArray, string) method builds a JsonObject whose data-csvariants string serializes variant details for multiple entries.

Parameters (JsonArray overload)

NameTypeRequiredDescription
entriesJsonArrayNoCDA entries. Returns { "data-csvariants": "[]" } if null.
contentTypeUidstringYes (if entries provided)Shared content type UID for the entries. Required whenever entries are not null.

Returns

ValueDescription
JsonObjectContains data-csvariants as a string (serialized JSON array). Each element represents { entry_uid, contenttype_uid, variants }.

Validation

  • Throws ArgumentException if entries are provided, and contentTypeUid is null or empty.

Behavior

  • Calls GetVariantAliases and serializes the result into data-csvariants.
  • Returns { "data-csvariants": "[]" } if entries is null. It does not throw an error.

Note The data-csvariants is returned as raw JSON text. In standard Razor or Blazor attribute binding, output is HTML-encoded automatically. If you inject this value into raw HTML or bypass framework encoding, encode it for an HTML attribute before rendering.

Example

The following example builds the attribute for multiple entries:

// Initialize the utility and generate metadata tags for multiple entries
var variantUtils = new VariantUtility();
JsonArray entries = GetEntriesFromListResponse();
string contentTypeUid = "product";

try {
    JsonObject attributeMap = variantUtils.GetVariantMetadataTags(entries, contentTypeUid);
    string jsonString = attributeMap["data-csvariants"].ToString();
    // Output: "[{\"entry_uid\":\"...\"},{\"entry_uid\":\"...\"}]"
} catch (ArgumentException ex) {
    Console.WriteLine(ex.Message);
}

Deprecated Methods

The following methods are deprecated and will be removed in a future release:

  • GetDataCsvariantsAttribute(JsonObject, string)
  • GetDataCsvariantsAttribute(JsonArray, string)

Note Use GetVariantMetadataTags instead. These methods internally delegate to GetVariantMetadataTags and return the same result, ensuring backward compatibility.