Azure Table storage output bindings for Azure Functions

Use an Azure Table storage output binding to write entities to a table in an Azure Storage account.

Note

This output binding does not support updating existing entities. Use the TableOperation.Replace operation from the Azure Storage SDK to update an existing entity.

Example

The following example shows a C# function that uses an HTTP trigger to write a single table row.

public class TableStorage
{
    public class MyPoco
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public string Text { get; set; }
    }

    [FunctionName("TableOutput")]
    [return: Table("MyTable")]
    public static MyPoco TableOutput([HttpTrigger] dynamic input, ILogger log)
    {
        log.LogInformation($"C# http trigger function processed: {input.Text}");
        return new MyPoco { PartitionKey = "Http", RowKey = Guid.NewGuid().ToString(), Text = input.Text };
    }
}

Attributes and annotations

In C# class libraries, use the TableAttribute.

The attribute's constructor takes the table name. The attribute can be used on an out parameter or on the return value of the function, as shown in the following example:

[FunctionName("TableOutput")]
[return: Table("MyTable")]
public static MyPoco TableOutput(
    [HttpTrigger] dynamic input, 
    ILogger log)
{
    ...
}

You can set the Connection property to specify the storage account to use, as shown in the following example:

[FunctionName("TableOutput")]
[return: Table("MyTable", Connection = "StorageConnectionAppSetting")]
public static MyPoco TableOutput(
    [HttpTrigger] dynamic input, 
    ILogger log)
{
    ...
}

For a complete example, see the C# example.

You can use the StorageAccount attribute to specify the storage account at class, method, or parameter level. For more information, see Input - attributes.

Configuration

The following table explains the binding configuration properties that you set in the function.json file and the Table attribute.

function.json property Attribute property Description
type n/a Must be set to table. This property is set automatically when you create the binding in the Azure portal.
direction n/a Must be set to out. This property is set automatically when you create the binding in the Azure portal.
name n/a The variable name used in function code that represents the table or entity. Set to $return to reference the function return value.
tableName TableName The name of the table.
partitionKey PartitionKey The partition key of the table entity to write. See the usage section for guidance on how to use this property.
rowKey RowKey The row key of the table entity to write. See the usage section for guidance on how to use this property.
connection Connection The name of an app setting that contains the Storage connection string to use for this binding. If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here. For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "MyStorage". If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage.

When you're developing locally, app settings go into the local.settings.json file.

Usage

Access the output table entity by using a method parameter ICollector<T> paramName or IAsyncCollector<T> paramName where T includes the PartitionKey and RowKey properties. These properties are often accompanied by implementing ITableEntity or inheriting TableEntity.

Alternatively you can use a CloudTable method parameter to write to the table by using the Azure Storage SDK. If you try to bind to CloudTable and get an error message, make sure that you have a reference to the correct Storage SDK version.

Exceptions and return codes

Binding Reference
Table Table Error Codes
Blob, Table, Queue Storage Error Codes
Blob, Table, Queue Troubleshooting

Next steps