PreviewToken
PreviewToken
The PreviewToken class creates and deletes the preview token of one delivery token. A preview token gives the Live Preview panel access to your website content.
When to Use This Class: Use this class to set up or revoke Live Preview access for a delivery token.
Conceptual Role: Acts as the Content Management API (CMA) client for the /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>/preview_token endpoint. Call stack.PreviewToken("<DELIVERY_TOKEN_UID>") to get an instance.
Returns: PreviewToken. An object for the preview token of one delivery token.
Instance State
Get an instance with stack.PreviewToken("<DELIVERY_TOKEN_UID>"). The method takes a required delivery token UID, and every method works on the preview token of that delivery token:
- Call Create or CreateAsync to create the preview token.
- Call Delete or DeleteAsync to delete it. Neither method needs a preview token UID, because a delivery token has at most one preview token.
Fetch, FetchAsync, Update, and UpdateAsync come from the shared base class. They throw ArgumentException because the instance has no UID, and the CMA reference documents no matching request. The Method Index below lists every method.
Warning stack.PreviewToken() does not check the delivery token UID. A null or empty value builds the path /stacks/delivery_tokens//preview_token, and the API rejects the request that follows.
Class-Level Notes
Authentication.
- stack.PreviewToken() throws InvalidOperationException when the client has no authtoken and the stack has no management token.
- The CMA reference lists the authtoken as required for both preview token requests. It lists a management token in the authorization header as optional for create and as required for delete. The SDK sends one credential per request: the management token when the stack has one, else the authtoken.
Where the token works.
- The CMA reference states that preview tokens work only with the rest-preview.contentstack.com endpoint, which serves Live Preview content. The methods on this class call the Content Management API at the client's host, as every other class does.
Create with the delivery token.
- To create a delivery token and its preview token in one request, pass create_with_preview_token to the Create method of DeliveryToken.
Capability Matrix
| Capability | Supported | Notes / Alternative |
|---|---|---|
| Read or update a preview token | No | The CMA reference lists create and delete requests only. Delete and create the token again to change it. |
| Create a preview token together with its delivery token | Not on this class | Use the create_with_preview_token query parameter of DeliveryToken.Create. |
Method Index
Create and delete the preview token. Available on stack.PreviewToken("<DELIVERY_TOKEN_UID>").
| Method Name | Returns | Description |
|---|---|---|
| Create | The API response with the created preview token. | Creates the preview token of a delivery token. |
| CreateAsync | The API response with the created preview token. | Creates the preview token of a delivery token asynchronously. |
| Delete | The API response with the deletion notice. | Deletes the preview token of a delivery token. |
| DeleteAsync | The API response with the deletion notice. | Deletes the preview token of a delivery token asynchronously. |
Inherited methods with no documented request. Available on the same instance, and they throw unless you set Uid.
| Method Name | Returns | Description |
|---|---|---|
| Fetch | The API response with the requested preview token. | Sends a GET request to the preview token path. |
| FetchAsync | The API response with the requested preview token. | Sends a GET request to the preview token path asynchronously. |
| Update | The API response with the updated preview token. | Sends a PUT request to the preview token path. |
| UpdateAsync | The API response with the updated preview token. | Sends a PUT request to the preview token path asynchronously. |
Class-Level Snippet
This snippet creates the preview token of a delivery token, then deletes it.
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewToken previewToken = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Live Preview",
Description = "Preview token for the website Live Preview panel"
};
try
{
ContentstackResponse created = await previewToken.CreateAsync(model);
Console.WriteLine(created.OpenJsonObjectResponse().ToJsonString());
// Later, revoke the preview token. The delivery token stays in place.
await previewToken.DeleteAsync();
}
catch (ContentstackErrorException e)
{
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}Methods
Each method of the PreviewToken class has its own section below, with its parameters, validation rules, behavior, and examples.
Create
Create creates the preview token of one delivery token, for use in the Live Preview panel.
| Name | Type | Description |
|---|---|---|
| model (required) | PreviewTokenModel | Sets the name and description of the preview token. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: ContentstackResponse. The API response with the created preview token.
Validation
- Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
- The SDK does not check the delivery token UID you passed to stack.PreviewToken(). A null or empty value builds the path /stacks/delivery_tokens//preview_token, and the API rejects the request.
- Throws InvalidOperationException only if you set the Uid property of the instance first, because Create posts to the preview token path of a delivery token. stack.PreviewToken() sets no UID.
- Throws ContentstackErrorException when the API rejects the request, for example a delivery token UID that does not exist.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Sends one POST request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>/preview_token, with model serialized under a token key. See Create preview token in the Content Management API (CMA) reference.
- Leaves a null Name or Description out of the request body.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response.
Example
Basic usage: create a preview token for a delivery token
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Live Preview",
Description = "Preview token for the website Live Preview panel"
};
try
{
ContentstackResponse response = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").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 preview token with a query parameter collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel() { Name = "Live Preview" };
// 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>").PreviewToken("<DELIVERY_TOKEN_UID>").Create(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Edge case: passing a null model
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").Create(null!);
}
catch (ArgumentNullException e)
{
// Fires before any request, because the model is null
Console.WriteLine(e.Message);
}CreateAsync
CreateAsync creates the preview token of one delivery token, for use in the Live Preview panel, without blocking the calling thread.
| Name | Type | Description |
|---|---|---|
| model (required) | PreviewTokenModel | Sets the name and description of the preview token. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: Task<ContentstackResponse>. The API response with the created preview token.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
- The SDK does not check the delivery token UID you passed to stack.PreviewToken(). A null or empty value builds the path /stacks/delivery_tokens//preview_token, and the API rejects the request.
- Throws InvalidOperationException only if you set the Uid property of the instance first, because CreateAsync posts to the preview token path of a delivery token. stack.PreviewToken() sets no UID.
- 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 delivery token UID that does not exist.
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: create a preview token for a delivery token
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Live Preview",
Description = "Preview token for the website Live Preview panel"
};
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").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 preview token with a query parameter collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel() { Name = "Live Preview" };
// 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>").PreviewToken("<DELIVERY_TOKEN_UID>").CreateAsync(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Delete
Delete deletes the preview token of one delivery token.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters to the request. |
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.
- The SDK does not check the delivery token UID you passed to stack.PreviewToken(). A null or empty value builds the path /stacks/delivery_tokens//preview_token, and the API rejects the request.
- Delete needs no preview token UID, so it does not throw ArgumentException the way other Delete methods do.
- Throws ContentstackErrorException when the API rejects the request, for example a delivery token that has no preview token.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Sends one DELETE request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>/preview_token. See Delete preview token in the Content Management API (CMA) reference.
- Identifies the preview token by its delivery token. A delivery token has at most one preview token, so the path holds no preview token UID.
- Leaves the delivery token in place.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response.
Warning Deleting a preview token stops the Live Preview panel that uses it. The SDK offers no way to restore it. Create a new one with the Create method.
Example
Basic usage: delete the preview token of a delivery token
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_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 preview token with a query parameter collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
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>").PreviewToken("<DELIVERY_TOKEN_UID>").Delete(collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}DeleteAsync
DeleteAsync deletes the preview token of one delivery token, without blocking the calling thread.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters to the request. |
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.
- The SDK does not check the delivery token UID you passed to stack.PreviewToken(). A null or empty value builds the path /stacks/delivery_tokens//preview_token, and the API rejects the request.
- DeleteAsync needs no preview token UID, so it does not throw ArgumentException the way other Delete methods do.
- 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, for example a delivery token that has no preview token.
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 preview token stops the Live Preview panel that uses it. The SDK offers no way to restore it. Create a new one with the Create method.
Example
Basic usage: delete the preview token of a delivery token
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_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 preview token with a query parameter collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
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>").PreviewToken("<DELIVERY_TOKEN_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 Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient();
try
{
ContentstackResponse response = await client.Stack("<API_KEY>", "<MANAGEMENT_TOKEN>").PreviewToken("<DELIVERY_TOKEN_UID>").DeleteAsync();
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Fetch
Fetch comes from the shared base class and sends a GET request to the preview token path of a delivery token. It throws unless you set the Uid property first.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: ContentstackResponse. The API response with the requested preview token.
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) on every call unless you set the Uid property first, because Fetch comes from the shared base class, which checks for a UID. stack.PreviewToken() sets none.
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- PreviewToken inherits Fetch from the base class without changing it.
- When the instance has a UID, sends one GET request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>/preview_token. The path never contains the UID.
- The Content Management API (CMA) reference lists no request to get a preview token. It lists create and delete requests only.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response.
Limitations
- Does not read a preview token through a documented API request. Use the Fetch method of DeliveryToken to read the delivery token instead.
Example
Basic usage: calling Fetch on a preview token instance
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
// Fires ArgumentException, because stack.PreviewToken() sets no UID.
ContentstackResponse response = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").Fetch();
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ArgumentException e)
{
// Fires before any request, because the instance has no UID
Console.WriteLine(e.Message);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: set Uid, then call Fetch with a collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// The CMA reference lists no query parameters for a request to this path. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
PreviewToken previewToken = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>");
previewToken.Uid = "<PREVIEW_TOKEN_UID>";
// The SDK sends the request, but the CMA reference documents no such request.
ContentstackResponse response = previewToken.Fetch(collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}FetchAsync
FetchAsync comes from the shared base class and sends a GET request to the preview token path of a delivery token. It throws unless you set the Uid property first.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: Task<ContentstackResponse>. The API response with the requested preview token.
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) on every call unless you set the Uid property first, because FetchAsync comes from the shared base class, which checks for a UID. stack.PreviewToken() sets none.
- 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: calling FetchAsync on a preview token instance
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
// Fires ArgumentException, because stack.PreviewToken() sets no UID.
ContentstackResponse response = await client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").FetchAsync();
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ArgumentException e)
{
// Fires before any request, because the instance has no UID
Console.WriteLine(e.Message);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: set Uid, then call FetchAsync with a collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// The CMA reference lists no query parameters for a request to this path. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
PreviewToken previewToken = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>");
previewToken.Uid = "<PREVIEW_TOKEN_UID>";
// The SDK sends the request, but the CMA reference documents no such request.
ContentstackResponse response = await previewToken.FetchAsync(collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Update
Update comes from the shared base class and sends a PUT request to the preview token path of a delivery token. It throws unless you set the Uid property first.
| Name | Type | Description |
|---|---|---|
| model (required) | PreviewTokenModel | Sets the name and description to send. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: ContentstackResponse. The API response with the updated preview token.
Validation
- Throws ArgumentException (parameter uid) on every call unless you set the Uid property first, because Update comes from the shared base class, which checks for a UID. stack.PreviewToken() sets none.
- Throws ArgumentNullException (parameter dataModel) if model is null.
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- PreviewToken inherits Update from the base class without changing it.
- When the instance has a UID, sends one PUT request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>/preview_token, with model serialized under a token key. The path never contains the UID.
- The Content Management API (CMA) reference lists no request to update a preview token. It lists create and delete requests only.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response.
Limitations
- Does not change a preview token through a documented API request. Delete the preview token with Delete and create a new one with Create instead.
Example
Basic usage: calling Update on a preview token instance
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel() { Name = "Live Preview" };
try
{
// Fires ArgumentException, because stack.PreviewToken() sets no UID.
ContentstackResponse response = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").Update(model);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ArgumentException e)
{
// Fires before any request, because the instance has no UID
Console.WriteLine(e.Message);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: set Uid, then call Update with a collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel() { Name = "Live Preview" };
// The CMA reference lists no query parameters for a request to this path. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
PreviewToken previewToken = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>");
previewToken.Uid = "<PREVIEW_TOKEN_UID>";
// The SDK sends the request, but the CMA reference documents no such request.
ContentstackResponse response = previewToken.Update(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}UpdateAsync
UpdateAsync comes from the shared base class and sends a PUT request to the preview token path of a delivery token. It throws unless you set the Uid property first.
| Name | Type | Description |
|---|---|---|
| model (required) | PreviewTokenModel | Sets the name and description to send. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: Task<ContentstackResponse>. The API response with the updated preview token.
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) on every call unless you set the Uid property first, because UpdateAsync comes from the shared base class, which checks for a UID. stack.PreviewToken() sets none.
- Throws ArgumentNullException (parameter dataModel) if model is null.
- 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: calling UpdateAsync on a preview token instance
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel() { Name = "Live Preview" };
try
{
// Fires ArgumentException, because stack.PreviewToken() sets no UID.
ContentstackResponse response = await client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").UpdateAsync(model);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ArgumentException e)
{
// Fires before any request, because the instance has no UID
Console.WriteLine(e.Message);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: set Uid, then call UpdateAsync with a collection
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel() { Name = "Live Preview" };
// The CMA reference lists no query parameters for a request to this path. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
PreviewToken previewToken = client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>");
previewToken.Uid = "<PREVIEW_TOKEN_UID>";
// The SDK sends the request, but the CMA reference documents no such request.
ContentstackResponse response = await previewToken.UpdateAsync(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}