Table of Contents

Class UserDefinedFunctionProperties

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

Represents a user defined function in the Azure Cosmos service.

public class UserDefinedFunctionProperties
Inheritance
UserDefinedFunctionProperties
Inherited Members
Extension Methods

Examples

The following examples show how to register and use UDFs.

await this.container.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> feedIterator = this.container.Items.GetItemsQueryIterator<double>(
    sqlQueryDefinition: sqlQuery,
    partitionKey: "Done"))
{
    while (feedIterator.HasMoreResults)
    {
        foreach (var tax in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(tax);
        }
    }
}

Remarks

Azure Cosmos supports JavaScript user defined functions (UDFs) which are stored in the database and can be used inside queries. Refer to https://docs.microsoft.com/azure/cosmos-db/sql-api-sql-query#javascript-integration for how to use UDFs within queries. Refer to https://docs.microsoft.com/azure/cosmos-db/programming#udf for more details about implementing UDFs in JavaScript.

Constructors

UserDefinedFunctionProperties()

public UserDefinedFunctionProperties()

Properties

Body

Gets or sets the body of the user defined function for the Azure Cosmos DB service.

public string Body { get; set; }

Property Value

string

The body of the user defined function.

Remarks

This must be a valid JavaScript function e.g. "function (input) { return input.toLowerCase(); }".

ETag

Gets the entity tag associated with the resource from the Azure Cosmos DB service.

public string ETag { get; }

Property Value

string

The entity tag associated with the resource.

Remarks

ETags are used for concurrency checking when updating resources.

Id

Gets or sets the Id of the resource in the Azure Cosmos DB service.

public string Id { get; set; }

Property Value

string

The Id associated with the resource.

Remarks

Every resource within an Azure Cosmos DB database account needs to have a unique identifier. Unlike Microsoft.Azure.Documents.Resource.ResourceId, which is set internally, this Id is settable by the user and is not immutable.

The following characters are restricted and cannot be used in the Id property: '/', '\\', '?', '#'

Gets the self-link associated with the resource from the Azure Cosmos DB service.

public string SelfLink { get; }

Property Value

string

The self-link associated with the resource.

A self-link is a static addressable Uri for each resource within a database account and follows the Azure Cosmos DB resource model. E.g. a self-link for a document could be dbs/db_resourceid/colls/coll_resourceid/documents/doc_resourceid