Jack Dalton
Jack Dalton

Reputation: 3681

Getting a complete list of all column names from Azure Table storage programmatically

I'm writing a web portal to interface with Azure.

I want to pull out and display the contents of an Azure Table store (nosql)

I am using :

CloudTable table = TableClient.GetTableReference(tableName);
IEnumerable<DynamicTableEntity> results = table.ExecuteQuery(new TableQuery());

This fetches the table and the entities.

enter image description here

Each returned DynamicTableEntity only lists the properties that have values. Not having a list of all of the table properties makes displaying the data on a view troublesome.

Is there a way of getting all of the column names for the table programmatically using Microsoft.WindowsAzure.Storage.

Upvotes: 1

Views: 3523

Answers (2)

HUSSAIN
HUSSAIN

Reputation: 11

I have made below custom logic and it is working perfectly fine for me

public static async Task Main(string[] args)
        {
            List<string> properties = new List<string>();
            var acc = new CloudStorageAccount(
                         new StorageCredentials("YOUR STORAGE ACCOUNT NAME", "YOUR STORAGE ACCOUNT CONNECTION STRING"), true);
            var tableClient = acc.CreateCloudTableClient();
            CloudTable table = tableClient.GetTableReference("YOUR TABLE NAME");
            IEnumerable<DynamicTableEntity> results = table.ExecuteQuery(new TableQuery());
            foreach (var item in results)     
            {
                if(item.Properties != null && item.Properties.Count > 0)
                {
                    foreach(var property in item.Properties)
                    {
                        if(properties != null && properties.Count > 0)
                        {
                            string s1 = property.Key.ToString();
                            if(!properties.Contains(s1))
                            {
                                properties.Add(s1);
                            }
                        }
                        else
                        {
                            string s2 = property.Key.ToString();
                            properties.Add(s2);
                        } 
                    }
                }
            }
            if(properties != null && properties.Count > 0)
            {
                foreach(var item in properties)
                {
                    Console.WriteLine(item.ToString());
                }    
            }

partition key,row key and timestamp are common properties for all tables you can add by default while creating list

Upvotes: 1

Serdar Ozler
Serdar Ozler

Reputation: 3802

In Azure Storage, tables store data as collections of entities. An entity has a primary key and a set of properties. However, the Table service does not enforce any schema for tables, so two entities in the same table may have different sets of properties.

So, if the client application does not enforce a schema, there is no easy way to query the union of all properties of all entities in a table.

Upvotes: 2

Related Questions