none
Retrieve Azure Table Storage Entity in Azure Function App and Update one of the properties

    Question

  • I am relatively new to Azure Table Storage. I have alerts from Azure devices populating Azure Table Storage via a webhook each time an alert is triggered on a device  but  when alert has been resolved I'd like to update the "flag" property to "Closed" in table storage. There are lots of examples online on how to retrieve a record from Azure Table Storage but I can't seem to get them to work from inside the Azure Function App interface.

    Essentially what I am trying to do is, where AlertName = "xyz" and Flag = "Open", update flag to "Closed". It seems fairly simple but I cannot get it to work.

    Is anyone able to provide a sample of how I do this from within the Azure Function App interface (run.csx) in c#?

    public class TableItem

    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public string AlertName { get; set; }
        public string Status { get; set; }
        public string AlertTime { get; set; }
        public string ResourceName { get; set; }
        public string MetricValue { get; set; }
        public string MetricName { get; set; }
        public string WindowSize { get; set; }
        public string SubscriptionId { get; set; }
        public string ResourceGroupName { get; set; }
        public string Flag { get; set; }
    }

    Monday, October 24, 2016 2:16 AM

Answers

  • Hi Amanda,

     

    It seems that you’d like to query table entity and update the value of entity property in Azure function, and you could get the values of AlertName and flag property from HttpRequestMessage object req. As we know, entity's partition and row key uniquely identify the entity in the table, so you need to pass the values of partition and row key.

     

    Besides, performing Azure table storage in C# requires installing Azure Storage Client Library for .NET. To use NuGet packages in a C# function, we could upload a project.json file to the function's folder in the function app's file system.

    project.json

    {
      "frameworks": {
        "net46":{
          "dependencies": {
            "WindowsAzure.Storage": "7.2.1"
          }
        }
       }
    }
    

    And then we could add the required using statements to your run.csx file

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    

    and write code to retrieve the entity and update property value.

    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);
    
    //log.Info($"{data.PartitionKey} {data.RowKey}");
    
    string pk = data.PartitionKey;
    string rk = data.RowKey;
    
    
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("yourconnectstring");
    
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    CloudTable table = tableClient.GetTableReference("yourtablename");
    
    TableOperation retrieveOperation = TableOperation.Retrieve<TableItem>(pk, rk);
    
    TableResult retrievedResult = table.Execute(retrieveOperation);
    
    TableItem updateEntity = (TableItem)retrievedResult.Result;
    
    updateEntity.Flag = "Closed";
    
    TableOperation updateOperation = TableOperation.Replace(updateEntity);
    
    table.Execute(updateOperation);
    

    Thursday, October 27, 2016 1:26 AM

All replies

  • Hi Amanda,

    Thank you for posting here! We are happy to assist you.

    This article explains how to configure and code Azure Storage triggers and bindings in Azure Functions.

    For complete information on Azure Table storage. Click here

    Refer here; How Azure Functions C# developer reference

    Regards,
    Sumanth BM

    Monday, October 24, 2016 1:01 PM
    Moderator
  • Thank you for your response Sumanth. I have been through most of this documentation. Do you know if it is possible to the following in ONE function app:

    1. Receive Http trigger via webhook - (working)

    2. Store details of http request in (azure) table storage - (working)

    3. Lookup up the (azure) table storage to find record and edit an entity property based on the details of the http trigger that is received - (not working).

    I have just been reading through the following article https://azure.microsoft.com/en-us/documentation/articles/functions-reference/.

    Have I understood correctly that when a http request is used as a trigger you cannot bind an input for the function? This would also mean that I cannot do step 3 in my list above, because if I can't bind table storage, I also cannot query it?

    If my understanding is correct, would it be better to run a webjob to do step 3 or is there a better way to do this, maybe create a timer function or data processing function to query table storage and update the entity properties instead?

    Regards, 

    Amanda

    Tuesday, October 25, 2016 2:22 AM
  • Hi Amanda,

     

    It seems that you’d like to query table entity and update the value of entity property in Azure function, and you could get the values of AlertName and flag property from HttpRequestMessage object req. As we know, entity's partition and row key uniquely identify the entity in the table, so you need to pass the values of partition and row key.

     

    Besides, performing Azure table storage in C# requires installing Azure Storage Client Library for .NET. To use NuGet packages in a C# function, we could upload a project.json file to the function's folder in the function app's file system.

    project.json

    {
      "frameworks": {
        "net46":{
          "dependencies": {
            "WindowsAzure.Storage": "7.2.1"
          }
        }
       }
    }
    

    And then we could add the required using statements to your run.csx file

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    

    and write code to retrieve the entity and update property value.

    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);
    
    //log.Info($"{data.PartitionKey} {data.RowKey}");
    
    string pk = data.PartitionKey;
    string rk = data.RowKey;
    
    
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("yourconnectstring");
    
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    CloudTable table = tableClient.GetTableReference("yourtablename");
    
    TableOperation retrieveOperation = TableOperation.Retrieve<TableItem>(pk, rk);
    
    TableResult retrievedResult = table.Execute(retrieveOperation);
    
    TableItem updateEntity = (TableItem)retrievedResult.Result;
    
    updateEntity.Flag = "Closed";
    
    TableOperation updateOperation = TableOperation.Replace(updateEntity);
    
    table.Execute(updateOperation);
    

    Thursday, October 27, 2016 1:26 AM
  • This is not the answer because the question is for an Azure Function, and this is not Azure function code
    Wednesday, January 24, 2018 7:54 PM