Label

View as Markdown

Label

Label creates, fetches, lists, updates, and deletes the labels of a stack. A label groups content types that work together, and a label can sit under one or more parent labels.

When to Use This Class: Use this class when your code organizes content types. For example, a stack setup script creates a Marketing label and adds the blog_post content type to it.

Conceptual Role: Acts as the Content Management API (CMA) client for the /labels endpoint. Call stack.Label() to get an instance, and describe the label with a LabelModel object.

Returns: Label. A handle for label operations.

Instance State

The UID you pass to stack.Label() decides which methods you can call on the instance:

  • Call stack.Label() without a UID to work with the label collection, for example Query or Create.
  • Call stack.Label("<LABEL_UID>") to work with one label, 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-item method on an instance with no UID throws ArgumentException.

The Method Index below lists every method in each group.

Warning The SDK checks for an empty string as if it were no UID, so stack.Label("") allows Query and Create, and the SDK then sends the request to /labels/ with a trailing slash. Pass null, or call stack.Label() with no argument, when you mean the collection.

Class-Level Notes

Authentication.

  • stack.Label() throws InvalidOperationException when the client has no authtoken and the Stack has no management token, and when the Stack has no API key.
  • The CMA accepts an authtoken, a management token, or an Open Authorization (OAuth) token for every label request. When the Stack has a management token, the SDK sends it instead of the client's authtoken.

Request body.

  • Create and Update serialize the LabelModel object under the label key. The SDK leaves out every property that is null.
  • The API assigns the UID of a new label. Read it from the label key of the Create response.

Branch scoping.

  • Label requests accept a branch header. Create the Stack with client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>", "<BRANCH_UID>") to send every request of this class to that branch.

Errors.

  • The SDK throws ContentstackErrorException when the API returns an error status. Read StatusCode, ErrorCode, ErrorMessage, and Errors on the exception to see what the API rejected.

Rate Limiting & Retry.

  • This SDK automatically retries a request that returns 429, 500, 502, 503, or 504, with configurable delay and attempt limits.

Additional Resources For more information on retry settings, refer to the ContentstackClientOptions documentation. For the request shapes, refer to the Labels section of the Content Management API reference.

Method Index

Work with the label collection. Available on stack.Label() when you pass no UID.

Method NameReturnsDescription
QueryA new query for labels.Builds a query that lists labels.
CreateThe API response with the created label.Creates a label.
CreateAsyncThe API response with the created label.Creates a label asynchronously.

Work with one label. Available on stack.Label("<LABEL_UID>").

Method NameReturnsDescription
FetchThe API response with the requested label.Retrieves one label.
FetchAsyncThe API response with the requested label.Retrieves one label asynchronously.
UpdateThe API response with the updated label.Changes the name, parents, or content types of a label.
UpdateAsyncThe API response with the updated label.Changes a label asynchronously.
DeleteThe API response with the deletion notice.Deletes a label.
DeleteAsyncThe API response with the deletion notice.Deletes a label asynchronously.

Class-Level Snippet

The snippet below creates a Marketing label for two content types, reads the UID that the API assigns, and lists every label in the 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>");
Stack stack = client.Stack("<API_KEY>");

LabelModel model = new LabelModel
{
    Name = "Marketing",
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

try
{
    ContentstackResponse created = await stack.Label().CreateAsync(model);
    Console.WriteLine($"New label UID: {created.OpenJsonObjectResponse()["label"]?["uid"]}");

    ContentstackResponse list = await stack.Label().Query().IncludeCount().FindAsync();
    Console.WriteLine($"Labels in the stack: {list.OpenJsonObjectResponse()["count"]}");
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

Methods

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

Create

Create creates a label in the stack from a LabelModel object.

NameTypeDescription
model (required)LabelModel

Name, parent labels, and content types of the label.

Default: Not applicable
collectionParameterCollection

Adds query parameters, for example include_branch.

Default: null

Returns: ContentstackResponse. The API response with the created label.

Validation

  • Throws InvalidOperationException if the instance has a UID, because Create adds a new label to the collection. Call stack.Label() with no UID.
  • Throws ArgumentNullException (parameter dataModel) if model is null, before any request.
  • The SDK does not check model. A missing name or an unknown UID in the model reaches the API, and the SDK throws the API rejection as ContentstackErrorException. Read e.Errors for the property that the API rejected.
  • collection set to null sends no query parameters.

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

Behavior

  • Sends one POST request to /labels with the body {"label": <model>}. The SDK leaves out every LabelModel property that is null.
  • The Add label request of the Content Management API reference takes the label name, the parent label UIDs, and the content type UIDs.
  • Returns the raw API response. The new label sits under the label key, and the API assigns its UID.

Example

Basic usage: create a label

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

LabelModel model = new LabelModel
{
    Name = "Marketing",
    ContentTypes = new List<string> { "blog_post" }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Label().Create(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["label"]?["uid"]);
}
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 label with every property and a query parameter, using a management token

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

LabelModel model = new LabelModel
{
    Name = "Campaigns",
    Parent = new List<string> { "<PARENT_LABEL_UID>" },
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Label().Create(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

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

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;

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

LabelModel model = new LabelModel { Name = "Marketing" };

try
{
    // Create works on the collection, so call stack.Label() with no UID.
    client.Stack("<API_KEY>").Label("<LABEL_UID>").Create(model);
}
catch (InvalidOperationException e)
{
    // Fires before any request, because the instance has a UID
    Console.WriteLine(e.Message);
}

CreateAsync

CreateAsync creates a label in the stack from a LabelModel object, without blocking the calling thread.

NameTypeDescription
model (required)LabelModel

Name, parent labels, and content types of the label.

Default: Not applicable
collectionParameterCollection

Adds query parameters, for example include_branch.

Default: null

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

Validation

  • Throws InvalidOperationException if the instance has a UID, because CreateAsync adds a new label to the collection. Call stack.Label() with no UID.
  • Throws ArgumentNullException (parameter dataModel) if model is null.
  • The SDK throws the exceptions above when you call CreateAsync, before it returns a task. An API rejection surfaces when you await the task, as ContentstackErrorException.

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

Behavior

  • Sends the same POST request as Create, on a task that you await.
  • Otherwise behaves like Create.

Example

Basic usage: create a label

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

LabelModel model = new LabelModel
{
    Name = "Marketing",
    ContentTypes = new List<string> { "blog_post" }
};

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Label().CreateAsync(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["label"]?["uid"]);
}
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 label with every property and a query parameter, using a management token

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

LabelModel model = new LabelModel
{
    Name = "Campaigns",
    Parent = new List<string> { "<PARENT_LABEL_UID>" },
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Label().CreateAsync(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Delete

Delete deletes the label whose UID you passed to stack.Label().

NameTypeDescription
collectionParameterCollection

Adds query parameters to the request.

Default: null

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

Validation

  • Throws ArgumentException (parameter uid) if the instance has no UID or an empty string, because Delete removes one label. Call stack.Label("<LABEL_UID>").
  • The SDK does not check the UID format. A UID that names no label reaches the API, and the SDK throws the API rejection as ContentstackErrorException.
  • collection set to null sends no query parameters.

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

Behavior

  • Sends one DELETE request to /labels/<LABEL_UID>, with the entries of collection as query parameters.
  • The Delete label request of the Content Management API reference deletes the label. The reference lists no query parameters for this request.
  • Returns the raw API response with a notice key.

Warning Deleting a label is permanent. The Content Management API reference lists no request that restores a deleted label.

Example

Basic usage: delete a label

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;

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

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Label("<LABEL_UID>").Delete();
    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}");
}

All parameters: delete a label with a query parameter collection, using a management token

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;

ContentstackClient client = new ContentstackClient();

ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
        .Label("<LABEL_UID>")
        .Delete(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

DeleteAsync

DeleteAsync deletes the label whose UID you passed to stack.Label(), 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 ArgumentException (parameter uid) if the instance has no UID or an empty string, because DeleteAsync removes one label. Call stack.Label("<LABEL_UID>").
  • The SDK throws the exception above when you call DeleteAsync, before it returns a task.
  • An API rejection surfaces when you await the task, as ContentstackErrorException.

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

Behavior

  • Sends the same DELETE request as Delete, on a task that you await.
  • Otherwise behaves like Delete.

Warning Deleting a label is permanent. The Content Management API reference lists no request that restores a deleted label.

Example

Basic usage: delete a label

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;

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

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Label("<LABEL_UID>").DeleteAsync();
    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}");
}

All parameters: delete a label with a query parameter collection, using a management token

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;

ContentstackClient client = new ContentstackClient();

ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
        .Label("<LABEL_UID>")
        .DeleteAsync(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Fetch

Fetch retrieves the label whose UID you passed to stack.Label().

NameTypeDescription
collectionParameterCollection

Adds query parameters, for example include_branch.

Default: null

Returns: ContentstackResponse. The API response with the requested label.

Validation

  • Throws ArgumentException (parameter uid) if the instance has no UID or an empty string, because Fetch reads one label. Call stack.Label("<LABEL_UID>").
  • The SDK does not check the UID format. A UID that names no label reaches the API, and the SDK throws the API rejection as ContentstackErrorException.
  • collection set to null sends no query parameters.

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

Behavior

  • Sends one GET request to /labels/<LABEL_UID>, with the entries of collection as query parameters.
  • The Get a single label request of the Content Management API reference accepts query parameters on this request. include_branch set to true adds the _branch key to the response.
  • Returns the raw API response. The label sits under the label key. Read it as JSON with OpenJsonObjectResponse(), or deserialize the label node into a LabelModel object.

Example

Basic usage: fetch a label by UID

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;

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

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Label("<LABEL_UID>").Fetch();
    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: fetch a label with a query parameter, using a management token

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;

ContentstackClient client = new ContentstackClient();

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
        .Label("<LABEL_UID>")
        .Fetch(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["label"]?["name"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Typed result: read the label into LabelModel

using System;
using System.Text.Json;
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>").Label("<LABEL_UID>").Fetch();

    // The label sits under the "label" key, so deserialize that node.
    LabelModel? model = response.OpenJsonObjectResponse()["label"]?.Deserialize<LabelModel>(client.SerializerOptions);
    Console.WriteLine(model?.Name);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

FetchAsync

FetchAsync retrieves the label whose UID you passed to stack.Label(), without blocking the calling thread.

NameTypeDescription
collectionParameterCollection

Adds query parameters, for example include_branch.

Default: null

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

Validation

  • Throws ArgumentException (parameter uid) if the instance has no UID or an empty string, because FetchAsync reads one label. Call stack.Label("<LABEL_UID>").
  • The SDK throws the exception above when you call FetchAsync, before it returns a task.
  • An API rejection surfaces when you await the task, as ContentstackErrorException.

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

Behavior

  • Sends the same GET request as Fetch, on a task that you await.
  • Otherwise behaves like Fetch.

Example

Basic usage: fetch a label by UID

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;

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

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Label("<LABEL_UID>").FetchAsync();
    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: fetch a label with a query parameter, using a management token

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;

ContentstackClient client = new ContentstackClient();

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
        .Label("<LABEL_UID>")
        .FetchAsync(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["label"]?["name"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Query

Query returns a query object that lists the labels in the stack when you call Find or FindAsync on it.

This method takes no parameters.

Returns: Query. A new query for labels.

Validation

  • Throws InvalidOperationException if the instance has a UID, because Query lists the label collection. Call stack.Label() with no UID.
  • Query sends no request. Errors that do occur come from the Find or FindAsync call that follows, not from Query. The SDK throws an API rejection as ContentstackErrorException.

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

Behavior

  • Find and FindAsync send one GET request to /labels. The request carries the parameters from Limit, Skip, IncludeCount, and the collection argument of Find. See the Query class for those methods.
  • The Get all labels request of the Content Management API reference accepts include_count, include_branch, and a query filter in JSON. Pass a filter with ParameterCollection.AddQuery.
  • Returns the raw API response. The labels sit under the labels key, and count holds the total when you call IncludeCount.
  • Does not page through the results for you. Without Limit and Skip, the API applies its own page size.

Example

Basic usage: list the labels in a stack

using System;
using System.Text.Json.Nodes;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;

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

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Label().Query().Find();
    JsonArray? items = response.OpenJsonObjectResponse()["labels"]?.AsArray();

    foreach (JsonNode? item in items ?? new JsonArray())
    {
        Console.WriteLine($"{item?["uid"]}: {item?["name"]}");
    }
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}

Paging: read one page of labels with a count, using a management token

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;

ContentstackClient client = new ContentstackClient();

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
        .Label()
        .Query()
        .Limit(10)
        .Skip(0)
        .IncludeCount()
        .FindAsync(collection);
    Console.WriteLine($"Total labels: {response.OpenJsonObjectResponse()["count"]}");
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Filter: find labels by name

using System;
using System.Text.Json.Nodes;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;

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

ParameterCollection collection = new ParameterCollection();
collection.AddQuery(new JsonObject { ["name"] = "Marketing" });

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Label().Query().Find(collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["labels"]?.AsArray().Count);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

Update

Update changes the label whose UID you passed to stack.Label().

NameTypeDescription
model (required)LabelModel

The new values of the label.

Default: Not applicable
collectionParameterCollection

Adds query parameters, for example include_branch.

Default: null

Returns: ContentstackResponse. The API response with the updated label.

Validation

  • Throws ArgumentException (parameter uid) if the instance has no UID or an empty string, because Update changes one label. Call stack.Label("<LABEL_UID>").
  • Throws ArgumentNullException (parameter dataModel) if model is null, before any request.
  • The SDK does not check model. An invalid value reaches the API, and the SDK throws the API rejection as ContentstackErrorException.

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

Behavior

  • Sends one PUT request to /labels/<LABEL_UID> with the body {"label": <model>}. The SDK leaves out every LabelModel property that is null.
  • The Update label request of the Content Management API reference replaces the name, parent labels, and content types of the label with the values you send.
  • Returns the raw API response. The updated label sits under the label key.

Example

Basic usage: update a label

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

LabelModel model = new LabelModel
{
    Name = "Marketing Pages",
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Label("<LABEL_UID>").Update(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["label"]?["name"]);
}
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 label and add a query parameter, using a management token

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

LabelModel model = new LabelModel
{
    Name = "Campaigns",
    Parent = new List<string> { "<PARENT_LABEL_UID>" },
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Label("<LABEL_UID>").Update(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}

UpdateAsync

UpdateAsync changes the label whose UID you passed to stack.Label(), without blocking the calling thread.

NameTypeDescription
model (required)LabelModel

The new values of the label.

Default: Not applicable
collectionParameterCollection

Adds query parameters, for example include_branch.

Default: null

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

Validation

  • Throws ArgumentException (parameter uid) if the instance has no UID or an empty string, because UpdateAsync changes one label. Call stack.Label("<LABEL_UID>").
  • Throws ArgumentNullException (parameter dataModel) if model is null.
  • The SDK throws the exceptions above when you call UpdateAsync, before it returns a task. An API rejection surfaces when you await the task, as ContentstackErrorException.

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

Behavior

  • Sends the same PUT request as Update, on a task that you await.
  • Otherwise behaves like Update.

Example

Basic usage: update a label

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

LabelModel model = new LabelModel
{
    Name = "Marketing Pages",
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>").Label("<LABEL_UID>").UpdateAsync(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["label"]?["name"]);
}
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 label and add a query parameter, using a management token

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

LabelModel model = new LabelModel
{
    Name = "Campaigns",
    Parent = new List<string> { "<PARENT_LABEL_UID>" },
    ContentTypes = new List<string> { "blog_post", "landing_page" }
};

ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);

try
{
    ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").Label("<LABEL_UID>").UpdateAsync(model, collection);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    Console.WriteLine(e.ErrorMessage);
}