PublishRule

View as Markdown

PublishRule

The PublishRule class creates, fetches, lists, updates, and deletes the publish rules of a stack. A publish rule makes publishing or unpublishing at a workflow stage wait for approval.

When to Use This Class: Use this class to require approval before entries of a content type publish to an environment.

Conceptual Role: Acts as the Content Management API (CMA) client for the /workflows/publishing_rules endpoint. Call stack.Workflow().PublishRule() to get an instance.

Returns: PublishRule. An object for publish rule operations.

Instance State

Get an instance from the PublishRule method of Workflow. The UID you pass to it decides which methods you can call:

  • Call stack.Workflow().PublishRule() without a UID to work with the publish rule collection, for example FindAll or Create.
  • Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>") to work with one publish rule, for example Fetch or Delete.

If you call a method from the other group, it throws before any request leaves the SDK. A collection method on an instance that has a UID throws InvalidOperationException, and a single-rule method on an instance with no UID throws ArgumentException. The Method Index below lists every method in each group.

The workflow UID you pass to stack.Workflow() has no effect here. Name the workflow in PublishRuleModel.WorkflowUid instead.

Warning The SDK treats an empty string as no UID for its checks, but still adds it to the request path. PublishRule("") allows Create and FindAll, which then send their requests to /workflows/publishing_rules/ with a trailing slash. Call PublishRule() with no argument when you mean the collection.

Class-Level Notes

Authentication.

  • stack.Workflow() throws InvalidOperationException when the client has no authtoken and the stack has no management token.
  • The CMA reference accepts an authtoken, a management token, or an Open Authorization (OAuth) token for every publish rule request.

Requests and responses.

  • Every method returns the raw API response as a ContentstackResponse. A single rule sits under the publishing_rule key, and the list from FindAll sits under the publishing_rules key.
  • Create and Update serialize a PublishRuleModel under the publishing_rule key and leave out its null properties.

Capability Matrix

CapabilitySupportedNotes / Alternative
List the publish rules of one content type for an actionNot on this classUse the GetPublishRule method of Workflow.
Send, approve, or reject a publish requestNot on this classUse the PublishRequest method of Entry.

Method Index

Work with the publish rule collection. Available on stack.Workflow().PublishRule() when you pass no UID.

Method NameReturnsDescription
FindAllThe API response with the publish rules in the stack.Lists the publish rules of the stack.
FindAllAsyncThe API response with the publish rules in the stack.Lists the publish rules of the stack asynchronously.
CreateThe API response with the created publish rule.Creates a publish rule.
CreateAsyncThe API response with the created publish rule.Creates a publish rule asynchronously.

Work with one publish rule. Available on stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").

Method NameReturnsDescription
FetchThe API response with the requested publish rule.Retrieves one publish rule.
FetchAsyncThe API response with the requested publish rule.Retrieves one publish rule asynchronously.
UpdateThe API response with the updated publish rule.Replaces the details of a publish rule.
UpdateAsyncThe API response with the updated publish rule.Replaces the details of a publish rule asynchronously.
DeleteThe API response with the deletion notice.Deletes a publish rule.
DeleteAsyncThe API response with the deletion notice.Deletes a publish rule asynchronously.

Class-Level Snippet

This snippet creates a publish rule for a workflow stage, then fetches the new rule by its UID.

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>");
Stack stack = client.Stack("<API_KEY>");

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

try
{
    ContentstackResponse created = await stack.Workflow().PublishRule().CreateAsync(model);
    string? ruleUid = created.OpenJsonObjectResponse()["publishing_rule"]?["uid"]?.GetValue<string>();

    ContentstackResponse rule = await stack.Workflow().PublishRule(ruleUid).FetchAsync();
    Console.WriteLine(rule.OpenJsonObjectResponse()["publishing_rule"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

Methods

Each method of the PublishRule class has its own section below, with its parameters, validation rules, behavior, and examples.

Create

Create creates a publish rule for a workflow stage, with the approvers, content types, locales, and environment you set in the model.

NameTypeDescription
model (required)PublishRuleModel

Sets the workflow stage, approvers, and scope of the rule.

Default: Not applicable
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: ContentstackResponse. The API response with the created publish rule.

Validation

  • Throws InvalidOperationException if the instance has a UID, because Create posts to the publish rule collection. Call stack.Workflow().PublishRule() with no UID.
  • Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
  • The SDK does not check the properties of model. The API validates them.
  • Throws ContentstackErrorException when the API rejects the request, for example a workflow stage UID that does not belong to the workflow.

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

Behavior

  • Sends one POST request to /workflows/publishing_rules, with model serialized under a publishing_rule key. See Create publish rules in the Content Management API (CMA) reference.
  • Leaves every null property of model out of the request body.
  • Always sends disable_approver_publishing, because DisableApproval is a bool that defaults to false.
  • Adds the entries of collection to the query string. null or an empty collection adds none.
  • Returns the raw API response. The created rule sits under the publishing_rule key.

Example

Basic usage: require approval before publishing blog posts

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    Actions = new List<string> { "publish" },
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Branches = new List<string> { "main" },
    Approvers = new Approvals()
    {
        Users = new List<string> { "<USER_UID>" },
        Roles = new List<string> { "<ROLE_UID>" }
    },
    DisableApproval = false
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule().Create(model);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: create a publish rule with a query parameter collection

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

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule().Create(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Edge case: calling Create on an instance that has a UID

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

try
{
    // Create adds a new publish rule, so call it on PublishRule() with no UID.
    client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Create(model);
}
catch (InvalidOperationException e)
{
    // Fires before any request, because the instance has a UID
    Console.WriteLine(e.Message);
}

CreateAsync

CreateAsync creates a publish rule for a workflow stage, with the approvers, content types, locales, and environment you set in the model, without blocking the calling thread.

NameTypeDescription
model (required)PublishRuleModel

Sets the workflow stage, approvers, and scope of the rule.

Default: Not applicable
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: Task<ContentstackResponse>. The API response with the created publish rule.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws InvalidOperationException if the instance has a UID, because CreateAsync posts to the publish rule collection. Call stack.Workflow().PublishRule() with no UID.
  • Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
  • The SDK does not check the properties of model. The API validates them.
  • The SDK throws the client-side exceptions above when you call CreateAsync, before it returns a task. An API rejection surfaces when you await the task.
  • Throws ContentstackErrorException when the API rejects the request, for example a workflow stage UID that does not belong to the workflow.

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

Behavior

  • Completes the task when the API responds.
  • Otherwise behaves like Create.

Example

Basic usage: require approval before publishing blog posts

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    Actions = new List<string> { "publish" },
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Branches = new List<string> { "main" },
    Approvers = new Approvals()
    {
        Users = new List<string> { "<USER_UID>" },
        Roles = new List<string> { "<ROLE_UID>" }
    },
    DisableApproval = false
};

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule().CreateAsync(model);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: create a publish rule with a query parameter collection

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

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule().CreateAsync(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Delete

Delete deletes one publish rule.

NameTypeDescription
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: ContentstackResponse. The API response with the deletion notice.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws ArgumentException (parameter uid) if the instance has no UID, because Delete removes one publish rule. Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Sends one DELETE request to /workflows/publishing_rules/<PUBLISH_RULE_UID>. See Delete publish rules in the Content Management API (CMA) reference.
  • Adds the entries of collection to the query string. null or an empty collection adds none.
  • Returns the raw API response.

Warning Deleting a publish rule removes its approval requirement from the workflow stage. The SDK offers no way to restore the rule.

Example

Basic usage: delete a publish rule

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

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Delete();
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: delete a publish rule with a query parameter collection

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

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

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Delete(collection);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

DeleteAsync

DeleteAsync deletes one publish rule, without blocking the calling thread.

NameTypeDescription
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: Task<ContentstackResponse>. The API response with the deletion notice.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws ArgumentException (parameter uid) if the instance has no UID, because DeleteAsync removes one publish rule. Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").
  • The SDK throws the client-side exceptions above when you call DeleteAsync, before it returns a task. An API rejection surfaces when you await the task.
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Completes the task when the API responds.
  • Otherwise behaves like Delete.

Warning Deleting a publish rule removes its approval requirement from the workflow stage. The SDK offers no way to restore the rule.

Example

Basic usage: delete a publish rule

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

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").DeleteAsync();
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: delete a publish rule with a query parameter collection

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

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

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").DeleteAsync(collection);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Authentication: use a management token instead of an authtoken

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

ContentstackClient client = new ContentstackClient();

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Workflow().PublishRule("<PUBLISH_RULE_UID>").DeleteAsync();
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Fetch

Fetch retrieves the details of one publish rule.

NameTypeDescription
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: ContentstackResponse. The API response with the requested publish rule.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws ArgumentException (parameter uid) if the instance has no UID, because Fetch reads one publish rule. Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Sends one GET request to /workflows/publishing_rules/<PUBLISH_RULE_UID>. See Get a single publish rule in the Content Management API (CMA) reference.
  • Adds the entries of collection to the query string. null or an empty collection adds none.
  • Returns the raw API response. The rule sits under the publishing_rule key.

Example

Basic usage: fetch a publish rule

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

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Fetch();
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rule"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: fetch a publish rule with a query parameter collection

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

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

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Fetch(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rule"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Authentication: use a management token instead of an authtoken

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

ContentstackClient client = new ContentstackClient();

try
{
    ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Fetch();
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rule"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

FetchAsync

FetchAsync retrieves the details of one publish rule, without blocking the calling thread.

NameTypeDescription
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: Task<ContentstackResponse>. The API response with the requested publish rule.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws ArgumentException (parameter uid) if the instance has no UID, because FetchAsync reads one publish rule. Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").
  • The SDK throws the client-side exceptions above when you call FetchAsync, before it returns a task. An API rejection surfaces when you await the task.
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Completes the task when the API responds.
  • Otherwise behaves like Fetch.

Example

Basic usage: fetch a publish rule

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

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").FetchAsync();
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rule"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: fetch a publish rule with a query parameter collection

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

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

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").FetchAsync(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rule"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

FindAll

FindAll retrieves the publish rules of the stack.

NameTypeDescription
collectionParameterCollection?

Adds query parameters, for example content_types and include_count.

Default: null

Returns: ContentstackResponse. The API response with the publish rules in the stack.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws InvalidOperationException if the instance has a UID, because FindAll lists the publish rule collection. Call stack.Workflow().PublishRule() with no UID.
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Sends one GET request to /workflows/publishing_rules. See Get all publish rules in the Content Management API (CMA) reference.
  • The CMA reference lists the following three query parameters for this request. The SDK adds none of them for you:
    • content_types (required): the content type UIDs to filter the rules by.
    • limit (optional): the maximum number of rules to return.
    • include_count (required): true adds the total number of rules to the response.
  • Adds the entries of collection to the query string. null or an empty collection adds none.
  • Returns the raw API response. The publish rules sit under the publishing_rules key.

Example

Basic usage: list the publish rules of a stack

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

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule().FindAll();
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rules"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: list the rules of one content type with a count

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

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

ParameterCollection collection = new ParameterCollection();
collection.Add("content_types", "blog_post");
collection.Add("limit", 10);
collection.Add("include_count", true);

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule().FindAll(collection);
    Console.WriteLine($"Total rules: {response.OpenJsonObjectResponse()["count"]}");
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Authentication: use a management token instead of an authtoken

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

ContentstackClient client = new ContentstackClient();

try
{
    ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Workflow().PublishRule().FindAll();
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rules"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

FindAllAsync

FindAllAsync retrieves the publish rules of the stack, without blocking the calling thread.

NameTypeDescription
collectionParameterCollection?

Adds query parameters, for example content_types and include_count.

Default: null

Returns: Task<ContentstackResponse>. The API response with the publish rules in the stack.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws InvalidOperationException if the instance has a UID, because FindAllAsync lists the publish rule collection. Call stack.Workflow().PublishRule() with no UID.
  • The SDK throws the client-side exceptions above when you call FindAllAsync, before it returns a task. An API rejection surfaces when you await the task.
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Completes the task when the API responds.
  • Otherwise behaves like FindAll.

Example

Basic usage: list the publish rules of a stack

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

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule().FindAllAsync();
    Console.WriteLine(response.OpenJsonObjectResponse()["publishing_rules"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: list the rules of one content type with a count

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

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

ParameterCollection collection = new ParameterCollection();
collection.Add("content_types", "blog_post");
collection.Add("limit", 10);
collection.Add("include_count", true);

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule().FindAllAsync(collection);
    Console.WriteLine($"Total rules: {response.OpenJsonObjectResponse()["count"]}");
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Update

Update replaces the details of a publish rule with the values in the model.

NameTypeDescription
model (required)PublishRuleModel

Sets the full rule, including its approvers and scope.

Default: Not applicable
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: ContentstackResponse. The API response with the updated publish rule.

Validation

  • Throws ArgumentException (parameter uid) if the instance has no UID, because Update changes one publish rule. Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").
  • Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Sends one PUT request to /workflows/publishing_rules/<PUBLISH_RULE_UID>, with model serialized under a publishing_rule key. See Update publish rules in the Content Management API (CMA) reference.
  • Leaves every null property of model out of the request body.
  • Always sends disable_approver_publishing, because DisableApproval is a bool that defaults to false. A model built without DisableApproval = true turns that setting off.
  • Adds the entries of collection to the query string. null or an empty collection adds none.
  • Returns the raw API response. The updated rule sits under the publishing_rule key.

Example

Basic usage: add an approver role to a publish rule

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    Actions = new List<string> { "publish" },
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Branches = new List<string> { "main" },
    Approvers = new Approvals()
    {
        Users = new List<string> { "<USER_UID>" },
        Roles = new List<string> { "<ROLE_UID>" }
    },
    DisableApproval = false
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Update(model);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: update a publish rule with a query parameter collection

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

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").Update(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

UpdateAsync

UpdateAsync replaces the details of a publish rule with the values in the model, without blocking the calling thread.

NameTypeDescription
model (required)PublishRuleModel

Sets the full rule, including its approvers and scope.

Default: Not applicable
collectionParameterCollection?

Adds query parameters to the request.

Default: null

Returns: Task<ContentstackResponse>. The API response with the updated publish rule.

Validation

  • Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
  • Throws ArgumentException (parameter uid) if the instance has no UID, because UpdateAsync changes one publish rule. Call stack.Workflow().PublishRule("<PUBLISH_RULE_UID>").
  • Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
  • The SDK throws the client-side exceptions above when you call UpdateAsync, before it returns a task. An API rejection surfaces when you await the task.
  • Throws ContentstackErrorException when the API rejects the request.

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

Behavior

  • Completes the task when the API responds.
  • Otherwise behaves like Update.

Example

Basic usage: add an approver role to a publish rule

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    Actions = new List<string> { "publish" },
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Branches = new List<string> { "main" },
    Approvers = new Approvals()
    {
        Users = new List<string> { "<USER_UID>" },
        Roles = new List<string> { "<ROLE_UID>" }
    },
    DisableApproval = false
};

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").UpdateAsync(model);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

All parameters: update a publish rule with a query parameter collection

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

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

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Workflow().PublishRule("<PUBLISH_RULE_UID>").UpdateAsync(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Authentication: update with a management token

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

ContentstackClient client = new ContentstackClient();

PublishRuleModel model = new PublishRuleModel()
{
    WorkflowUid = "<WORKFLOW_UID>",
    WorkflowStageUid = "<WORKFLOW_STAGE_UID>",
    ContentTypes = new List<string> { "blog_post" },
    Locales = new List<string> { "en-us" },
    Environment = "<ENVIRONMENT_UID>",
    Approvers = new Approvals() { Roles = new List<string> { "<ROLE_UID>" } }
};

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Workflow().PublishRule("<PUBLISH_RULE_UID>").UpdateAsync(model);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}