Share via


IDocumentClient.CreateTriggerAsync Method

Definition

Overloads

CreateTriggerAsync(String, Trigger, RequestOptions)

Creates a trigger as an asychronous operation in the Azure Cosmos DB service.

CreateTriggerAsync(Uri, Trigger, RequestOptions)

Creates a trigger as an asychronous operation in the Azure Cosmos DB service.

CreateTriggerAsync(String, Trigger, RequestOptions)

Creates a trigger as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Trigger>> CreateTriggerAsync (string collectionLink, Microsoft.Azure.Documents.Trigger trigger, Microsoft.Azure.Documents.Client.RequestOptions options = default);
abstract member CreateTriggerAsync : string * Microsoft.Azure.Documents.Trigger * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Trigger>>
Public Function CreateTriggerAsync (collectionLink As String, trigger As Trigger, Optional options As RequestOptions = Nothing) As Task(Of ResourceResponse(Of Trigger))

Parameters

collectionLink
String

The link of the DocumentCollection to create the trigger in. E.g. dbs/db_rid/colls/col_rid/

trigger
Trigger

The Trigger object to create.

options
RequestOptions

(Optional) Any RequestOptionsfor this request.

Returns

A task object representing the service response for the asynchronous operation.

Exceptions

If either collectionLink or trigger is not set.

Represents a consolidation of failures that occured during async processing. Look within InnerExceptions to find the actual exception(s)

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the request supplied. It is likely that an Id was not supplied for the new trigger or that the Body was malformed.
403Forbidden - You have reached your quota of triggers for the collection supplied. Contact support to have this quota increased.
409Conflict - This means a Trigger with an id matching the id you supplied already existed.
413RequestEntityTooLarge - This means the body of the Trigger you tried to create was too large.

Examples

//Create a trigger that validates the contents of a document as it is created and adds a 'timestamp' property if one was not found.
Trigger trig = await client.CreateTriggerAsync(collectionLink, new Trigger
{
    Id = "ValidateDocuments",
    Body = @"function validate() {
                        var context = getContext();
                        var request = context.getRequest();                                                             
                        var documentToCreate = request.getBody();

                        // validate properties
                        if (!('timestamp' in documentToCreate)) {
                            var ts = new Date();
                            documentToCreate['timestamp'] = ts.getTime();
                        }

                        // update the document that will be created
                        request.setBody(documentToCreate);
                      }",
    TriggerType = TriggerType.Pre,
    TriggerOperation = TriggerOperation.Create
});

See also

Applies to

CreateTriggerAsync(Uri, Trigger, RequestOptions)

Creates a trigger as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Trigger>> CreateTriggerAsync (Uri documentCollectionUri, Microsoft.Azure.Documents.Trigger trigger, Microsoft.Azure.Documents.Client.RequestOptions options = default);
abstract member CreateTriggerAsync : Uri * Microsoft.Azure.Documents.Trigger * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Trigger>>
Public Function CreateTriggerAsync (documentCollectionUri As Uri, trigger As Trigger, Optional options As RequestOptions = Nothing) As Task(Of ResourceResponse(Of Trigger))

Parameters

documentCollectionUri
Uri

The URI of the document collection to create the trigger in.

trigger
Trigger

The Trigger object.

options
RequestOptions

(Optional) The RequestOptions for the request.

Returns

The task object representing the service response for the asynchronous operation.

Applies to