CosmosScripts.CreateUserDefinedFunctionAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a user defined function as an asynchronous operation in the Azure Cosmos DB service.
public abstract System.Threading.Tasks.Task<Azure.Response<Azure.Cosmos.Scripts.UserDefinedFunctionProperties>> CreateUserDefinedFunctionAsync (Azure.Cosmos.Scripts.UserDefinedFunctionProperties userDefinedFunctionProperties, Azure.Cosmos.RequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateUserDefinedFunctionAsync : Azure.Cosmos.Scripts.UserDefinedFunctionProperties * Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.Cosmos.Scripts.UserDefinedFunctionProperties>>
Public MustOverride Function CreateUserDefinedFunctionAsync (userDefinedFunctionProperties As UserDefinedFunctionProperties, Optional requestOptions As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of UserDefinedFunctionProperties))
Parameters
- userDefinedFunctionProperties
- UserDefinedFunctionProperties
The UserDefinedFunctionProperties object.
- requestOptions
- RequestOptions
(Optional) The options for the user defined function request RequestOptions
- cancellationToken
- CancellationToken
(Optional) CancellationToken representing request cancellation.
Returns
A task object representing the service response for the asynchronous operation.
Exceptions
If userDefinedFunctionProperties
is not set.
Represents a consolidation of failures that occurred 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 user defined function are:
StatusCode | Reason for exception |
---|---|
400 | BadRequest - This means something was wrong with the request supplied. It is likely that an Id was not supplied for the new user defined function or that the Body was malformed. |
403 | Forbidden - You have reached your quota of user defined functions for the collection supplied. Contact support to have this quota increased. |
409 | Conflict - This means a UserDefinedFunctionProperties with an id matching the id you supplied already existed. |
413 | RequestEntityTooLarge - This means the body of the UserDefinedFunctionProperties you tried to create was too large. |
Examples
This creates a user defined function then uses the function in an item query.
CosmosScripts 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");
await foreach (double tax in this.container.Items.GetItemsQueryIterator<double>(
sqlQueryDefinition: sqlQuery,
partitionKey: "Done"))
{
Console.WriteLine(tax);
}