Validation
The following validation applies to the parameters above:
- variantHeaders (string overload): Passing null silently omits the x-cs-variant-uid header and the request proceeds without a variant filter. Passing an empty string sets the header to an empty value.
- variantHeaders (List<string> overload): Passing null throws ArgumentNullException. Passing an empty list sets the x-cs-variant-uid header to an empty string.
Behavior
When Contentstack Personalize creates a variant in the CMS, it assigns a Variant Alias to identify it. Pass either the alias or the variant UID in variantHeaders to retrieve content populated with the matching variant data. The value is mapped to the x-cs-variant-uid HTTP request header before Fetch() executes the request.
- Each call to Fetch() maps to a single HTTP request.
- When branch is omitted, the SDK uses the branch from the stack configuration. If no stack-level branch is configured, it defaults to "main".
The following examples illustrate these behaviors.
Implementation and examples
The following snippet shows the basic Variant call. It initializes the client using ContentstackOptions and fetches an entry with two variant UIDs applied.
using Contentstack.Core;
using Contentstack.Core.Models;
using Contentstack.Core.Configuration;
var options = new ContentstackOptions()
{
ApiKey = "API_KEY",
DeliveryToken = "DELIVERY_TOKEN",
Environment = "ENVIRONMENT"
};
ContentstackClient stack = new ContentstackClient(options);
try
{
Entry entry = await stack
.ContentType("CONTENT_TYPE_UID")
.Entry("ENTRY_UID")
.Variant(new List<string> { "VARIANT_UID1", "VARIANT_UID2" })
.Fetch<Entry>();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
The following examples demonstrate:
- Applying a single variant with an explicit branch
- Applying multiple variants with an explicit branch
- Omitting branch, which causes the SDK to fall back to the branch configured at the stack level, or "main" if no branch is configured
using Contentstack.Core;
using Contentstack.Core.Models;
using Contentstack.Core.Configuration;
var options = new ContentstackOptions()
{
ApiKey = "API_KEY",
DeliveryToken = "DELIVERY_TOKEN",
Environment = "ENVIRONMENT"
};
ContentstackClient stack = new ContentstackClient(options);
try
{
Entry singleVariant = await stack
.ContentType("CONTENT_TYPE_UID")
.Entry("ENTRY_UID")
.Variant("VARIANT_UID1", "BRANCH_UID")
.Fetch<Entry>();
var variantUids = new List<string> { "VARIANT_UID1", "VARIANT_UID2" };
Entry multipleVariants = await stack
.ContentType("CONTENT_TYPE_UID")
.Entry("ENTRY_UID")
.Variant(variantUids, "BRANCH_UID")
.Fetch<Entry>();
Entry fallback = await stack
.ContentType("CONTENT_TYPE_UID")
.Entry("ENTRY_UID")
.Variant("VARIANT_UID1")
.Fetch<Entry>();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
IMPORTANT: The parameters passed to .Variant(string variantHeader, string branch) are positional. The SDK does not validate whether the values are supplied in the correct order.
If the arguments are reversed, the SDK treats the first value as the variant identifier and the second value as the branch name. No SDK validation error is thrown, which can result in an unexpected API error that does not clearly indicate the root cause.
Always pass the variant UID (or alias) as the first argument and the branch name as the second argument.