PublishVariant

View as Markdown

PublishVariant

PublishVariant names one entry variant, and the version of it, in a publish or unpublish request. You add it to the Variants list of PublishUnpublishDetails.

When to Use This Class: Use this class when you publish or unpublish a specific version of an entry variant, alone or together with the base entry.

Conceptual Role: Acts as the data model that the SDK sends as one item of the entry.variants array of a publish or unpublish request.

Returns: PublishVariant. One entry variant and its version.

Properties

NameTypeRequiredDefaultDescription
Uidstring?NonullUID of the variant. Sent as uid.
Versionint?NonullVersion of the variant. Sent as version.

The VariantRules property of PublishUnpublishDetails takes a PublishVariantRules object, with the properties in the table below:

NameTypeRequiredDefaultDescription
PublishLatestBasebool?NonullPublishes the latest base entry. Sent as publish_latest_base.
PublishLatestBaseConditionallybool?NonullPublishes the latest base entry conditionally. Sent as publish_latest_base_conditionally.

Class-Level Notes

What the SDK sends.

  • The SDK writes uid and version for each item only when they are not null. It writes each rule of PublishVariantRules only when it is not null.
  • The SDK does not check that the UID names a variant or that the version exists.

What the API expects.

  • The Content Management API (CMA) reference asks for the variant UID and version within variants. To publish a variant, it also asks for publish_latest_base_conditionally set to true within variant_rules. See Publish entry variant.
  • The API ignores a base entry version in a variant publish request, and publishes the variant based on the latest base version or as variant_rules specifies.

Entry and EntryVariant.

  • With the Publish method of Entry, list every variant you want in Variants yourself.
  • The Publish method of EntryVariant adds its own variant to Variants without a version, unless the list already holds an item with that UID. Add your own item to send a version.

Method Index

PublishVariant and PublishVariantRules have no methods. Set their properties and assign them to PublishUnpublishDetails.

Class-Level Snippet

The snippet below publishes version 2 of one entry variant together with the latest base entry.

using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");

PublishUnpublishDetails details = new PublishUnpublishDetails
{
    Environments = new List<string> { "<ENVIRONMENT_NAME>" },
    Locales = new List<string> { "en-us" },
    Variants = new List<PublishVariant>
    {
        new PublishVariant { Uid = "<VARIANT_UID>", Version = 2 }
    },
    VariantRules = new PublishVariantRules
    {
        PublishLatestBase = false,
        PublishLatestBaseConditionally = true
    }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").ContentType("<CONTENT_TYPE_UID>").Entry("<ENTRY_UID>").Publish(details);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}