# Adding an item to a Azure Storage Table

In case you are new to the Azure Storage Tables, we've reviewed the following items this week:

Today, we'll be taking a look at reading an item through code that we previously placed into an Azure Storage Table.

# Getting Started

Open the C# Console application that we were working with yesterday (opens new window) and let's add two methods to:

  • Return all messages in a table
  • Lookup a message based off of the RowKey and PartitionKey

# Return all messages in a table

In our Program.cs file, we'll now add in a helper method to return all messages in a given table.

static void GetAllMessages(TableClient table)
    {
        Pageable<Thanks> queryResults = table.Query<Thanks>(ent => ent.PartitionKey.Equals("ThanksApp"));

        Console.WriteLine("GetAllMessages begin");
        // Iterate the <see cref="Pageable"> to access all queried entities.
        foreach (Thanks entity in queryResults)
        {
            Console.WriteLine(entity.Name);
            Console.WriteLine(entity.Date);
        }

        Console.WriteLine("GetAllMessages ends");
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

In this example, we'll pass in the given table name and passed on the PartitionKey it will return all messsages. In this example, we hardcoded the value to be "ThanksApp".

# Lookup a message based off of the RowKey and PartitionKey

Again in our Program.cs file, we'll now add another helper method to return a message based off of the RowKey and PartitionKey that we supply.

static void GetMessage(TableClient table, string partitionKey, string rowKey)
{
    //Please refer to https://docs.microsoft.com/en-us/rest/api/storageservices/querying-tables-and-entities for more details about query syntax.
    var queryResult = table.Query<Thanks>(filter: $"PartitionKey eq '{partitionKey}' and RowKey eq '{rowKey}'").Single();

    Console.WriteLine(queryResult.Date);
}
1
2
3
4
5
6
7

In this example, we'll pass in the table name, a partition key and a row key to return a message.

# Putting it all together

The Main method inside of the Program.cs file just needs to call the methods as shown below:

static void Main(string[] args)
{
    var serviceClient = new TableServiceClient(ConfigurationManager.AppSettings["StorageConnection"]);

    TableClient table = serviceClient.GetTableClient("thankfulfor");
    table.CreateIfNotExists();

    //added this line
    GetMessage(table, "ThanksApp", "I am thankful for the time with my family");
    GetAllMessages(table);
    //added this line
    Console.ReadKey();
}
1
2
3
4
5
6
7
8
9
10
11
12
13