Share via


Windows Phone 7.5 : Working with Azure Storage Table

If you have Windows Azure Table Storage and you want to access that from your phone then the best one to me to use the proxy or OData. Because then you will be able to control the number of rows as Phone has limited capacity and Azure Table Storage is massive. However, you can directly access the Table Storage from your phone application but in that case you need to hardcode your 512 bit secret key which is the golden pass to your Azure Table Storage account and you will not be doing it for sure. In a separate post I will demonstrate the capability of exposing your Windows Azure Table data as OData. Here I will show how you can add record from Windows Phone to your Windows Azure Table Storage.

Now to do that I need to create Windows Phone Application and add one small component from NuGet.

image

After it opens then run this command

Install-Package Phone.Storage

Once the assemblies are added to the project, let us do few cleanup job. Under the folder called “App_Start” there will be a C# code file called “StorageInitializer.cs”. Delete it as we will be doing it in the same page.

In that file it basically initializes the connection to Windows Azure Storage where we need to pass the account name and secret key with the URLs. Also we need two main namespaces to be added

 using Microsoft.WindowsAzure.Samples.Phone.Storage;
using System.Data.Services.Client;

After that initialize the connection,

 var resolver = new CloudStorageClientResolverAccountAndKey(
    new StorageCredentialsAccountAndKey("storageacc", "XYZKEYYYYYY"),
    new Uri("https://storageacc.blob.core.windows.net"),
    new Uri("https://storageacc.queue.core.windows.net"),
    new Uri("https://storageacc.table.core.windows.net"),
    Deployment.Current.Dispatcher);

CloudStorageContext.Current.Resolver = resolver;

After that the Entity structure will have to be created

 public class Employee : TableServiceEntity
{
    public string EmpName { get; set; }
}

Now assume in a button’s click you are saving the data.

 string tName = "Employee";
private void btnSave_Click(object sender, RoutedEventArgs e)
{                        
    var tableClient = CloudStorageContext.Current.Resolver.CreateCloudTableClient();
    tableClient.CreateTableIfNotExist(tName,
        p =>
        {
            var contextTable = CloudStorageContext.Current.Resolver.CreateTableServiceContext();
        });

    var empData = new Employee()
    {                
        PartitionKey = "Dev",
        RowKey = Guid.NewGuid().ToString(),
        Timestamp = DateTime.Now,
        EmpName = txtVal.Text
    };
    var ctx = tableClient.GetDataServiceContext();
    ctx.AddObject(tName, empData);

    ctx.BeginSaveChanges(asyncData => { var sRes = ctx.EndSaveChanges(asyncData); }, null);
    MessageBox.Show("Saved..");
    
}

That’s it!!! Isn’t it so cool?

Tips:

  • If the assembly downloads but does not get added to your project then probably you are running older version of NuGet. Please update it from Extension Manager.
  • Always pass the PartitionKey value first while creating the instance of the entity.
  • Never hardcode your storage key to Phone App. Have some Web Role created in Azure or in some hosted environment to interact with Azure Storage.
  • Use TableServiceEntity as base class for the Entity to avoid declaring the three mandatory properties and adding the attribute to indicate what is PartitionKey and RowKey.

For more in details discussion please refer to Windows Azure Toolkit for Phone at https://watwp.codeplex.com/ 

Namoskar!!!

Comments

  • Anonymous
    February 15, 2012
    Wah, kya baat hai?

  • Anonymous
    April 18, 2012
    Great sample! Very clean and usefull! Thanks a lot!

  • Anonymous
    April 18, 2012
    Glad you liked it.

  • Anonymous
    June 29, 2012
    n what is  the way if i want to read the entities from my windows phone ?? You have only told about storing not about accessing

  • Anonymous
    December 04, 2012
    But how to get data from storage??