Jake Conkerton-Darby
Jake Conkerton-Darby

Reputation: 1101

When using Event Grid trigger for Azure Function can I access data properties in binding expressions?

I'm trying to write an Azure Function which has an Event Grid trigger and a CosmosDB input binding. I'd like to use the data from the triggering event grid event to define part of a query so I only get the documents I need from Cosmos.

Example event data:

{
    "referenceId": "id-43"
}

Current function definition in C#:

public static void Run(
    [EventGridEvent] @event,
    [CosmosDB(
        databaseName: "%DatabaseName%",
        collectionName: "%CollectionName%",
        ConnectionStringSetting = "CosmosConnectionString",
        SqlQuery = "SELECT * FROM root WHERE root.otherDocId = '{data.referenceId}'"
    )] IEnumerable<Document> documents,
    ILogger logger)
{
    // Function Body
}

At the moment I'm stuck with errors saying Error while accessing 'referenceId': property does not exist. when I try to post to the function. Is there a way I can change things up to use the data as part of a binding expression within the SQL query?

Edit:

I am aware that the data properties can be bound as we have another function that binds to the Id and PartitionKey properties of the CosmosDB attribute. For example:

public static void Run(
    [EventGridEvent] @event,
    [CosmosDB(
        databaseName: "%DatabaseName%",
        collectionName: "%CollectionName%",
        ConnectionStringSetting = "CosmosConnectionString",
        Id = "{data.idProperty}",
        PartitionKey = "{data.partitionKeyProperty}"
    )] Document document,
    ILogger logger)
{
    // Function Body
}

Unfortunately I'm not able to use the partition key in this instance to filter the documents and it has to be via an SQL query.

Upvotes: 0

Views: 550

Answers (1)

Jake Conkerton-Darby
Jake Conkerton-Darby

Reputation: 1101

After further digging this appears to be a known issue, as can be seen via this issue on the azure-webjobs-sdk-extensions GitHub page: https://github.com/Azure/azure-webjobs-sdk-extensions/issues/595

There is a particular attribute on the SqlQuery property that controls how parameter binding is handled, and does not work for nested properties. Most people encounter this with query parameters for Http triggers, but the same issue applies in this instance.

Upvotes: 1

Related Questions