Table of Contents

Class Scripts

Namespace
Microsoft.Azure.Cosmos.Scripts
Assembly
Microsoft.Azure.Cosmos.Client.dll

Represents script operations on an Azure Cosmos container.

public abstract class Scripts
Inheritance
Scripts
Inherited Members
Extension Methods

Constructors

Scripts()

protected Scripts()
See Also

Methods

CreateStoredProcedureAsync(StoredProcedureProperties, RequestOptions, CancellationToken)

Creates a stored procedure as an asynchronous operation in the Azure Cosmos DB service.

public abstract Task<StoredProcedureResponse> CreateStoredProcedureAsync(StoredProcedureProperties storedProcedureProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

storedProcedureProperties StoredProcedureProperties

The Stored Procedure to create.

requestOptions RequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<StoredProcedureResponse>

The StoredProcedureProperties that was created contained within a Task object representing the service response for the asynchronous operation.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

string sprocBody = @"function simple(prefix)
   {
       var collection = getContext().getCollection();

       // Query documents and take 1st item.
       var isAccepted = collection.queryDocuments(
       collection.getSelfLink(),
       'SELECT * FROM root r',
       function(err, feed, options) {
           if (err)throw err;

           // Check the feed and if it's empty, set the body to 'no docs found',
           // Otherwise just take 1st element from the feed.
           if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
           else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]));
       });

       if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
   }";

Scripts scripts = this.container.Scripts;
StoredProcedureProperties storedProcedure = new StoredProcedureProperties(id, sprocBody);
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(storedProcedure);

// Execute the stored procedure
CosmosItemResponse<string> sprocResponse = await scripts.ExecuteStoredProcedureAsync<string, string>(
                              id, 
                              "Item as a string: ", 
                              new PartitionKey(testPartitionId));
Console.WriteLine("sprocResponse.Resource");
See Also

CreateTriggerAsync(TriggerProperties, RequestOptions, CancellationToken)

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

public abstract Task<TriggerResponse> CreateTriggerAsync(TriggerProperties triggerProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

triggerProperties TriggerProperties

The TriggerProperties object.

requestOptions RequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<TriggerResponse>

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

Examples

This creates a trigger then uses the trigger in a create item.

Scripts scripts = this.container.Scripts;
TriggerResponse triggerResponse = await scripts.CreateTriggerAsync(
    new TriggerProperties
    {
        Id = "addTax",
        Body = @"function AddTax() {
            var item = getContext().getRequest().getBody();

            // calculate the tax.
            item.tax = item.cost * .15;

            // Update the request -- this is what is going to be inserted.
            getContext().getRequest().setBody(item);
        }",
        TriggerOperation = TriggerOperation.All,
        TriggerType = TriggerType.Pre
    });

ItemRequestOptions options = new ItemRequestOptions()
{
    PreTriggers = new List<string>() { triggerResponse.Id },
};

// Create a new item with trigger set in the request options
ItemResponse<dynamic> createdItem = await this.container.Items.CreateItemAsync<dynamic>(item.status, item, options);
double itemTax = createdItem.Resource.tax;
See Also

CreateUserDefinedFunctionAsync(UserDefinedFunctionProperties, RequestOptions, CancellationToken)

Creates a user defined function as an asynchronous operation in the Azure Cosmos DB service.

public abstract Task<UserDefinedFunctionResponse> CreateUserDefinedFunctionAsync(UserDefinedFunctionProperties userDefinedFunctionProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

userDefinedFunctionProperties UserDefinedFunctionProperties

The UserDefinedFunctionProperties object.

requestOptions RequestOptions

(Optional) The options for the user defined function request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<UserDefinedFunctionResponse>

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

Examples

This creates a user defined function then uses the function in an item query.

Scripts scripts = this.container.Scripts;
await scripts.UserDefinedFunctions.CreateUserDefinedFunctionAsync(
    new UserDefinedFunctionProperties 
    { 
        Id = "calculateTax", 
        Body = @"function(amt) { return amt * 0.05; }" 
    });

QueryDefinition sqlQuery = new QueryDefinition(
    "SELECT VALUE udf.calculateTax(t.cost) FROM toDoActivity t where t.cost > @expensive and t.status = @status")
    .WithParameter("@expensive", 9000)
    .WithParameter("@status", "Done");

using (FeedIterator<double> setIterator = this.container.Items.GetItemsQueryIterator<double>(
    sqlQueryDefinition: sqlQuery,
    partitionKey: "Done")
{
    while (setIterator.HasMoreResults)
    {
        foreach (var tax in await setIterator.ReadNextAsync())
        {
            Console.WriteLine(tax);
        }
    }
}
See Also

DeleteStoredProcedureAsync(string, RequestOptions, CancellationToken)

Delete a StoredProcedureProperties from the Azure Cosmos DB service as an asynchronous operation.

public abstract Task<StoredProcedureResponse> DeleteStoredProcedureAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

id string

The identifier of the Stored Procedure to delete.

requestOptions RequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<StoredProcedureResponse>

A Task containing a ResponseMessage which will contain the response to the request issued.

Examples

This examples gets a reference to an existing stored procedure and deletes it.

Scripts scripts = this.container.Scripts;
StoredProcedureResponse response = await scripts.DeleteStoredProcedureAsync("taxUdfId");
See Also

DeleteTriggerAsync(string, RequestOptions, CancellationToken)

Delete a TriggerProperties from the Azure Cosmos service as an asynchronous operation.

public abstract Task<TriggerResponse> DeleteTriggerAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

id string

The id of the trigger to delete.

requestOptions RequestOptions

(Optional) The options for the trigger request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<TriggerResponse>

A Task containing a TriggerResponse which wraps a TriggerProperties which will contain information about the request issued.

Examples

This examples gets a reference to an existing trigger and deletes it.

Scripts scripts = this.container.Scripts;
TriggerResponse response = await scripts.DeleteTriggerAsync("existingId");
See Also

DeleteUserDefinedFunctionAsync(string, RequestOptions, CancellationToken)

Delete a UserDefinedFunctionProperties from the Azure Cosmos DB service as an asynchronous operation.

public abstract Task<UserDefinedFunctionResponse> DeleteUserDefinedFunctionAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

id string

The id of the user defined function to delete.

requestOptions RequestOptions

(Optional) The options for the user defined function request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<UserDefinedFunctionResponse>

A Task containing a UserDefinedFunctionResponse which wraps a UserDefinedFunctionProperties which will contain information about the request issued.

Examples

This examples gets a reference to an existing user defined function and deletes it.

Scripts scripts = this.container.Scripts;
UserDefinedFunctionResponse response = await this.container.DeleteUserDefinedFunctionAsync("existingId");
See Also

ExecuteStoredProcedureAsync<TOutput>(string, PartitionKey, dynamic[], StoredProcedureRequestOptions, CancellationToken)

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service.

public abstract Task<StoredProcedureExecuteResponse<TOutput>> ExecuteStoredProcedureAsync<TOutput>(string storedProcedureId, PartitionKey partitionKey, dynamic[] parameters, StoredProcedureRequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

storedProcedureId string

The identifier of the Stored Procedure to execute.

partitionKey PartitionKey

The partition key for the item.

parameters dynamic[]

(Optional) An array of dynamic objects representing the parameters for the stored procedure.

requestOptions StoredProcedureRequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<StoredProcedureExecuteResponse<TOutput>>

The task object representing the service response for the asynchronous operation which would contain any response set in the stored procedure.

Type Parameters

TOutput

The return type that is JSON serializable.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

string sprocBody = @"function simple(prefix, postfix)
   {
       var collection = getContext().getCollection();

       // Query documents and take 1st item.
       var isAccepted = collection.queryDocuments(
       collection.getSelfLink(),
       'SELECT * FROM root r',
       function(err, feed, options) {
           if (err)throw err;

           // Check the feed and if it's empty, set the body to 'no docs found',
           // Otherwise just take 1st element from the feed.
           if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
           else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
       });

       if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
   }";

Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
        sprocId,
        sprocBody);

// Execute the stored procedure
StoredProcedureExecuteResponse<string> sprocResponse = await scripts.ExecuteStoredProcedureAsync<string>(
                        sprocId,
                        new PartitionKey(testPartitionId),
                        new dynamic[] {"myPrefixString", "myPostfixString"});

Console.WriteLine(sprocResponse.Resource);
///
See Also

ExecuteStoredProcedureStreamAsync(string, PartitionKey, dynamic[], StoredProcedureRequestOptions, CancellationToken)

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service and obtains a Stream as response.

public abstract Task<ResponseMessage> ExecuteStoredProcedureStreamAsync(string storedProcedureId, PartitionKey partitionKey, dynamic[] parameters, StoredProcedureRequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

storedProcedureId string

The identifier of the Stored Procedure to execute.

partitionKey PartitionKey

The partition key for the item.

parameters dynamic[]

An array of dynamic objects representing the parameters for the stored procedure. This can be null if no parameters are required.

requestOptions StoredProcedureRequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<ResponseMessage>

The task object representing the service response for the asynchronous operation which would contain any response set in the stored procedure.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

string sprocBody = @"function simple(prefix, postfix)
   {
       var collection = getContext().getCollection();

       // Query documents and take 1st item.
       var isAccepted = collection.queryDocuments(
       collection.getSelfLink(),
       'SELECT * FROM root r',
       function(err, feed, options) {
           if (err)throw err;

           // Check the feed and if it's empty, set the body to 'no docs found',
           // Otherwise just take 1st element from the feed.
           if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
           else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
       });

       if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
   }";

Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
        sprocId,
        sprocBody);

// Execute the stored procedure
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
                        sprocId,
                        new PartitionKey(testPartitionId),
                        new dynamic[] {"myPrefixString", "myPostfixString"});

using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
    string stringResponse = await sr.ReadToEndAsync();
    Console.WriteLine(stringResponse);
 }

///
See Also

ExecuteStoredProcedureStreamAsync(string, Stream, PartitionKey, StoredProcedureRequestOptions, CancellationToken)

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service and obtains a Stream as response.

public abstract Task<ResponseMessage> ExecuteStoredProcedureStreamAsync(string storedProcedureId, Stream streamPayload, PartitionKey partitionKey, StoredProcedureRequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

storedProcedureId string

The identifier of the Stored Procedure to execute.

streamPayload Stream

A Stream containing the payload which should represent a JSON array or arraylike object of parameters. This is parsed using JSON.parse and Function.apply uses the result to call the stored procedure. This can be null if no parameters are required.

partitionKey PartitionKey

The partition key for the item.

requestOptions StoredProcedureRequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<ResponseMessage>

The task object representing the service response for the asynchronous operation which would contain any response set in the stored procedure. The response will contain status code (400) BadRequest if streamPayload represents anything other than a JSON array, object or null.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

string sprocBody = @"function simple(prefix, postfix)
   {
       var collection = getContext().getCollection();

       // Query documents and take 1st item.
       var isAccepted = collection.queryDocuments(
       collection.getSelfLink(),
       'SELECT * FROM root r',
       function(err, feed, options) {
           if (err)throw err;

           // Check the feed and if it's empty, set the body to 'no docs found',
           // Otherwise just take 1st element from the feed.
           if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
           else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
       });

       if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
   }";

Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
        sprocId,
        sprocBody);

// Serialize the parameters into a stream
string[] parameters = new string[] { "myPrefixString", "myPostfixString" };
byte[] serializedBytes = JsonSerializer.SerializeToUtf8Bytes(parameters);
MemoryStream streamPayload = new MemoryStream(serializedBytes);

// Execute the stored procedure
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
                        sprocId,
                        streamPayload,
                        new PartitionKey(testPartitionId));

using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
    string stringResponse = await sr.ReadToEndAsync();
    Console.WriteLine(stringResponse);
 }

///
See Also

GetStoredProcedureQueryIterator<T>(QueryDefinition, string, QueryRequestOptions)

This method creates a query for stored procedures under a container using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

public abstract FeedIterator<T> GetStoredProcedureQueryIterator<T>(QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryDefinition QueryDefinition

The cosmos SQL query definition.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator<T>

An iterator to read through the existing stored procedures.

Type Parameters

T

Examples

This create the type feed iterator for sproc with queryDefinition as input.

Scripts scripts = this.container.Scripts;
string queryText = "SELECT * FROM s where s.id like @testId";
QueryDefinition queryDefinition = new QueryDefinition(queryText);
queryDefinition.WithParameter("@testId", "testSprocId");
using (FeedIterator<StoredProcedureProperties> feedIterator = scripts.GetStoredProcedureQueryIterator<StoredProcedureProperties>(queryDefinition))
{
    while (feedIterator.HasMoreResults)
    {
        foreach (StoredProcedureProperties storedProcedure in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(storedProcedure.Id);
        }
    }
}
See Also

GetStoredProcedureQueryIterator<T>(string, string, QueryRequestOptions)

This method creates a query for stored procedures under a container using a SQL statement. It returns a FeedIterator.

public abstract FeedIterator<T> GetStoredProcedureQueryIterator<T>(string queryText = null, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryText string

The cosmos SQL query text.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator<T>

An iterator to read through the existing stored procedures.

Type Parameters

T

Examples

This create the type feed iterator for sproc with queryText as input.

Scripts scripts = this.container.Scripts;
using (FeedIterator<StoredProcedureProperties> feedIterator = this.scripts.GetStoredProcedureQueryIterator<StoredProcedureProperties>(
    "SELECT * FROM u where u.id like '%testId%'"))
{
    while (feedIterator.HasMoreResults)
    {
        foreach (StoredProcedureProperties properties in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(properties.Id);
        }
    }
}
See Also

GetStoredProcedureQueryStreamIterator(QueryDefinition, string, QueryRequestOptions)

This method creates a query for stored procedures under a container using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

public abstract FeedIterator GetStoredProcedureQueryStreamIterator(QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryDefinition QueryDefinition

The cosmos SQL query definition.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator

An iterator to read through the existing stored procedures.

Examples

This create the stream feed iterator for sproc with queryDefinition as input.

Scripts scripts = this.container.Scripts;
string queryText = "SELECT * FROM s where s.id like @testId";
QueryDefinition queryDefinition = new QueryDefinition(queryText);
queryDefinition.WithParameter("@testId", "testSprocId");
using (FeedIterator feedIterator = scripts.GetStoredProcedureQueryStreamIterator(queryDefinition))
{
    while (feedIterator.HasMoreResults)
    {
        // Stream iterator returns a response with status for errors
        using(ResponseMessage response = await feedIterator.ReadNextAsync())
        {
            // Handle failure scenario. 
            if(!response.IsSuccessStatusCode)
            {
                // Log the response.Diagnostics and handle the error
            }

            // Process the response.Content Stream
        }
    }
}
See Also

GetStoredProcedureQueryStreamIterator(string, string, QueryRequestOptions)

This method creates a query for stored procedures under a container using a SQL statement. It returns a FeedIterator.

public abstract FeedIterator GetStoredProcedureQueryStreamIterator(string queryText = null, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryText string

The cosmos SQL query text.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator

An iterator to read through the existing stored procedures.

Examples

This create the stream feed iterator for sproc with queryText as input.

Scripts scripts = this.container.Scripts;
string queryText = "SELECT * FROM s where s.id like '%testId%'";
using (FeedIterator feedIterator = this.scripts.GetStoredProcedureQueryStreamIterator(queryText)
{
    while (feedIterator.HasMoreResults)
    {
        // Stream iterator returns a response with status for errors
        using(ResponseMessage response = await feedIterator.ReadNextAsync())
        {
            // Handle failure scenario. 
            if(!response.IsSuccessStatusCode)
            {
                // Log the response.Diagnostics and handle the error
            }

            // Process the response.Content Stream
        }
    }
}
See Also

GetTriggerQueryIterator<T>(QueryDefinition, string, QueryRequestOptions)

This method creates a query for triggers under a container using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

public abstract FeedIterator<T> GetTriggerQueryIterator<T>(QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryDefinition QueryDefinition

The cosmos SQL query definition.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator<T>

An iterator to read through the existing stored procedures.

Type Parameters

T

Examples

This create the type feed iterator for Trigger with queryDefinition as input.

Scripts scripts = this.container.Scripts;
QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM t where t.id like @testId")
    .WithParameter("@testId", "testTriggerId");
using (FeedIterator<TriggerProperties> feedIterator = this.scripts.GetTriggerQueryIterator<TriggerProperties>(queryDefinition)
{
    while (feedIterator.HasMoreResults)
    {
        foreach (var properties in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(properties.Id);
        }
    }
}
See Also

GetTriggerQueryIterator<T>(string, string, QueryRequestOptions)

This method creates a query for triggers under a container using a SQL statement. It returns a FeedIterator.

public abstract FeedIterator<T> GetTriggerQueryIterator<T>(string queryText = null, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryText string

The cosmos SQL query text.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator<T>

An iterator to read through the existing stored procedures.

Type Parameters

T

Examples

This create the type feed iterator for Trigger with queryText as input.

Scripts scripts = this.container.Scripts;
using (FeedIterator<TriggerProperties> feedIterator = this.scripts.GetTriggerQueryIterator<TriggerProperties>(
    "SELECT * FROM t where t.id like '%testId%'")
{
    while (feedIterator.HasMoreResults)
    {
        foreach (var properties in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(properties.Id);
        }
    }
}
See Also

GetTriggerQueryStreamIterator(QueryDefinition, string, QueryRequestOptions)

This method creates a query for triggers under a container using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

public abstract FeedIterator GetTriggerQueryStreamIterator(QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryDefinition QueryDefinition

The cosmos SQL query definition.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator

An iterator to read through the existing stored procedures.

Examples

This create the stream feed iterator for Trigger with queryDefinition as input.

Scripts scripts = this.container.Scripts;\
QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM t where t.id like @testId")
 .WithParameter("@testId", "testTriggerId");
using (FeedIterator feedIterator = this.scripts.GetTriggerQueryStreamIterator(queryDefinition)
{
    while (feedIterator.HasMoreResults)
    {
        // Stream iterator returns a response with status for errors
        using(ResponseMessage response = await feedIterator.ReadNextAsync())
        {
            // Handle failure scenario. 
            if(!response.IsSuccessStatusCode)
            {
                // Log the response.Diagnostics and handle the error
            }

            // Process the response.Content Stream
        }
    }
}
See Also

GetTriggerQueryStreamIterator(string, string, QueryRequestOptions)

This method creates a query for triggers under a container using a SQL statement. It returns a FeedIterator.

public abstract FeedIterator GetTriggerQueryStreamIterator(string queryText = null, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryText string

The cosmos SQL query text.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator

An iterator to read through the existing stored procedures.

Examples

This create the stream feed iterator for Trigger with queryText as input.

Scripts scripts = this.container.Scripts;
string queryText = "SELECT * FROM t where t.id like '%testId%'";
using (FeedIterator iter = this.scripts.GetTriggerQueryStreamIterator(queryText)
{
}
See Also

GetUserDefinedFunctionQueryIterator<T>(QueryDefinition, string, QueryRequestOptions)

This method creates a query for user defined functions under a container using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

public abstract FeedIterator<T> GetUserDefinedFunctionQueryIterator<T>(QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryDefinition QueryDefinition

The cosmos SQL query definition.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator<T>

An iterator to read through the existing stored procedures.

Type Parameters

T

Examples

This create the type feed iterator for UDF with queryDefinition as input.

Scripts scripts = this.container.Scripts;
QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM u where u.id like @testId")
    .WithParameter("@testId", "testUDFId");
using (FeedIterator<UserDefinedFunctionProperties> feedIterator = this.scripts.GetUserDefinedFunctionQueryIterator<UserDefinedFunctionProperties>(queryDefinition)
{
    while (feedIterator.HasMoreResults)
    {
        foreach (var properties in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(properties.Id);
        }
    }
}
See Also

GetUserDefinedFunctionQueryIterator<T>(string, string, QueryRequestOptions)

This method creates a query for user defined functions under a container using a SQL statement. It returns a FeedIterator.

public abstract FeedIterator<T> GetUserDefinedFunctionQueryIterator<T>(string queryText = null, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryText string

The cosmos SQL query text.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator<T>

An iterator to read through the existing stored procedures.

Type Parameters

T

Examples

This create the type feed iterator for UDF with queryText as input.

Scripts scripts = this.container.Scripts;
using (FeedIterator<UserDefinedFunctionProperties> feedIterator = this.scripts.GetUserDefinedFunctionQueryIterator<UserDefinedFunctionProperties>(
    "SELECT * FROM u where u.id like '%testId%'")
{
    while (feedIterator.HasMoreResults)
    {
        foreach (var properties in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(properties.Id);
        }
    }
}
See Also

GetUserDefinedFunctionQueryStreamIterator(QueryDefinition, string, QueryRequestOptions)

This method creates a query for user defined functions under a container using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

public abstract FeedIterator GetUserDefinedFunctionQueryStreamIterator(QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryDefinition QueryDefinition

The cosmos SQL query definition.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator

An iterator to read through the existing stored procedures.

Examples

This create the stream feed iterator for UDF with queryDefinition as input.

Scripts scripts = this.container.Scripts;
QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM u where u.id like @testId")
  .WithParameter("@testId", "testUdfId");
using (FeedIterator feedIterator = this.scripts.GetUserDefinedFunctionQueryStreamIterator(queryDefinition)
{
    while (feedIterator.HasMoreResults)
    {
        // Stream iterator returns a response with status for errors
        using(ResponseMessage response = await feedIterator.ReadNextAsync())
        {
            // Handle failure scenario. 
            if(!response.IsSuccessStatusCode)
            {
                // Log the response.Diagnostics and handle the error
            }

            // Process the response.Content Stream
        }
    }
}
See Also

GetUserDefinedFunctionQueryStreamIterator(string, string, QueryRequestOptions)

This method creates a query for user defined functions under a container using a SQL statement. It returns a FeedIterator.

public abstract FeedIterator GetUserDefinedFunctionQueryStreamIterator(string queryText = null, string continuationToken = null, QueryRequestOptions requestOptions = null)

Parameters

queryText string

The cosmos SQL query text.

continuationToken string

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions QueryRequestOptions

(Optional) The options for the item query request.

Returns

FeedIterator

An iterator to read through the existing stored procedures.

Examples

This create the stream feed iterator for UDF with queryText as input.

Scripts scripts = this.container.Scripts;
using (FeedIterator feedIterator = this.scripts.GetUserDefinedFunctionQueryStreamIterator(
    "SELECT * FROM u where u.id like '%testId%'")
{
    while (feedIterator.HasMoreResults)
    {
        // Stream iterator returns a response with status for errors
        using(ResponseMessage response = await feedIterator.ReadNextAsync())
        {
            // Handle failure scenario. 
            if(!response.IsSuccessStatusCode)
            {
                // Log the response.Diagnostics and handle the error
            }

            // Process the response.Content Stream
        }
    }
}
See Also

ReadStoredProcedureAsync(string, RequestOptions, CancellationToken)

Reads a StoredProcedureProperties from the Azure Cosmos service as an asynchronous operation.

public abstract Task<StoredProcedureResponse> ReadStoredProcedureAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

id string

The identifier of the Stored Procedure to read.

requestOptions RequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<StoredProcedureResponse>

A Task containing a StoredProcedureProperties.

Examples

This reads an existing stored procedure.

Scripts scripts = this.container.Scripts;
StoredProcedureResponse storedProcedure = await scripts.ReadStoredProcedureAsync("ExistingId");
See Also

ReadTriggerAsync(string, RequestOptions, CancellationToken)

Reads a TriggerProperties from the Azure Cosmos service as an asynchronous operation.

public abstract Task<TriggerResponse> ReadTriggerAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

id string

The id of the trigger to read.

requestOptions RequestOptions

(Optional) The options for the trigger request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<TriggerResponse>

A Task containing a TriggerResponse which wraps a TriggerProperties containing the read resource record.

Examples

This reads an existing trigger

Scripts scripts = this.container.Scripts;
TriggerResponse response = await scripts.ReadTriggerAsync("ExistingId");
TriggerProperties triggerProperties = response;
See Also

ReadUserDefinedFunctionAsync(string, RequestOptions, CancellationToken)

Reads a UserDefinedFunctionProperties from the Azure Cosmos DB service as an asynchronous operation.

public abstract Task<UserDefinedFunctionResponse> ReadUserDefinedFunctionAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

id string

The id of the user defined function to read

requestOptions RequestOptions

(Optional) The options for the user defined function request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<UserDefinedFunctionResponse>

A Task containing a UserDefinedFunctionResponse which wraps a UserDefinedFunctionProperties containing the read resource record.

Examples

This reads an existing user defined function.

Scripts scripts = this.container.Scripts;
UserDefinedFunctionResponse response = await scripts.ReadUserDefinedFunctionAsync("ExistingId");
UserDefinedFunctionProperties udfProperties = response;
See Also

ReplaceStoredProcedureAsync(StoredProcedureProperties, RequestOptions, CancellationToken)

Replaces a StoredProcedureProperties in the Azure Cosmos service as an asynchronous operation.

public abstract Task<StoredProcedureResponse> ReplaceStoredProcedureAsync(StoredProcedureProperties storedProcedureProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

storedProcedureProperties StoredProcedureProperties

The Stored Procedure to replace

requestOptions RequestOptions

(Optional) The options for the stored procedure request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<StoredProcedureResponse>

A Task containing a StoredProcedureProperties.

Examples

This examples replaces an existing stored procedure.

//Updated body
string body = @"function AddTax() {
    var item = getContext().getRequest().getBody();

    // Validate/calculate the tax.
    item.tax = item.cost* .15;

    // Update the request -- this is what is going to be inserted.
    getContext().getRequest().setBody(item);
}";

Scripts scripts = this.container.Scripts;
StoredProcedureResponse response = await scripts.ReplaceStoredProcedureAsync(new StoredProcedureProperties("testTriggerId", body));
See Also

ReplaceTriggerAsync(TriggerProperties, RequestOptions, CancellationToken)

Replaces a TriggerProperties in the Azure Cosmos service as an asynchronous operation.

public abstract Task<TriggerResponse> ReplaceTriggerAsync(TriggerProperties triggerProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

triggerProperties TriggerProperties

The TriggerProperties object.

requestOptions RequestOptions

(Optional) The options for the trigger request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<TriggerResponse>

A Task containing a TriggerResponse which wraps a TriggerProperties containing the updated resource record.

Examples

This examples replaces an existing trigger.

TriggerProperties triggerProperties = new TriggerProperties
{
    Id = "testTriggerId",
    Body = @"function AddTax() {
        var item = getContext().getRequest().getBody();

        // Validate/calculate the tax.
        item.tax = item.cost* .15;

        // Update the request -- this is what is going to be inserted.
        getContext().getRequest().setBody(item);
    }",
    TriggerOperation = TriggerOperation.All,
    TriggerType = TriggerType.Post
};

Scripts scripts = this.container.Scripts;
TriggerResponse response = await scripts.ReplaceTriggerAsync(triggerSettigs);
See Also

ReplaceUserDefinedFunctionAsync(UserDefinedFunctionProperties, RequestOptions, CancellationToken)

Replaces a UserDefinedFunctionProperties in the Azure Cosmos DB service as an asynchronous operation.

public abstract Task<UserDefinedFunctionResponse> ReplaceUserDefinedFunctionAsync(UserDefinedFunctionProperties userDefinedFunctionProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)

Parameters

userDefinedFunctionProperties UserDefinedFunctionProperties

The UserDefinedFunctionProperties object.

requestOptions RequestOptions

(Optional) The options for the user defined function request.

cancellationToken CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<UserDefinedFunctionResponse>

A Task containing a UserDefinedFunctionResponse which wraps a UserDefinedFunctionProperties containing the updated resource record.

Examples

This examples replaces an existing user defined function.

Scripts scripts = this.container.Scripts;
UserDefinedFunctionProperties udfProperties = new UserDefinedFunctionProperties
{
    Id = "testUserDefinedFunId",
    Body = "function(amt) { return amt * 0.15; }",
};

UserDefinedFunctionResponse response = await scripts.ReplaceUserDefinedFunctionAsync(udfProperties);
UserDefinedFunctionProperties udfProperties = response;
See Also

See Also