Windows Azure Storage Client Library for Windows Phone: Part 1

Windows Azure Storage Client Library allows you to perform operations on Windows Azure storage from Windows Phone. Using Windows Azure Storage Client, you can

  1. Perform operations on Table
  2. Perform operations on Queue
  3. Perform Operations on BLOB

If you are not using Windows Azure Storage Client Library then to work with Windows Azure Storage you may need to create a WCF Service. Windows Phone will make a call to Service and Service will use Windows Azure Storage library to perform operations on Azure Storage. However using Windows Azure Storage Client for Windows Phone, you can directly perform operations on Windows Azure Storage.

You can get Windows Azure Storage Client Library and install it using NuGet. Read more about NuGet here. Once you have installed NuGet go ahead and create a Windows Phone 7.1 application by choosing target Windows Phone Version 7.1

image

After successful creation of project go to Tools then Library Package Manager, you will get option for Package Manager Console. Go ahead and select that option.

image

In bottom of Visual studio you will get Package Manage Console. Go ahead and install Phone.Storage package to the project.

You can install a package with below command

PM> Install-Package Phone.Storage

image

After successful installation of Phone.Storage package, you should have below references and file in solution explorer.

image

StorageInitializer.cs is very important class. In this class you can set whether you want to perform operation on local development fabric storage or on the azure storage. If you closely look into this file, you will find there are three sections.

  1. To configure to connect with local development fabric storage or Windows Azure Storage Emulator.
  2. To configure to connect with Windows Azure Web Role that contains Windows Azure Storage Proxies
  3. To configure to connect with Windows Azure Storage directly.

All the settings are commented but to work with storage of Windows Azure Emulator. If you want to work with Windows Azure Storage directly, you can do that by commenting third section of the file and providing Account Name and Account Key. Below is the uncommented section in file StorageInitalizer.cs. You need to provide your Windows Azure Storage account name and key.

image

Next let us say you want to create a table called Student. For that add a class called Student. Student entity class is inherting TableServiceEntity class.



using Microsoft.WindowsAzure.Samples.Phone.Storage;


namespace PhoneApp9
{
public class Student :TableServiceEntity
{
public string Name { get; set; }
public string RollNumber { get; set; }
public string Grade { get; set; }
}
}


Next as the design of the page, I have put a button and on click event of the button, Student table will get created if not exist and one row will get added.

Design of the page is as below,



<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button x:Name="btnCreateTable" Height="100" Content="Create Table" Margin="46,254,128,254" Click="btnCreateTable_Click" />
</Grid>
</Grid>


On the click event of the button, first you need to resolve table client as below. Set the table name.

clip_image002

After resolving Table client, you need to create table as below,

clip_image003

Once table is created you need to resolve the context to add rows in the table.

clip_image005

You can very much modify above code by getting value to be inserted from the user. In this case I am hardcoding the vale. On putting all codes together you should have below code behind.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.WindowsAzure.Samples.Phone.Storage;

namespace PhoneApp9
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void btnCreateTable_Click(object sender, RoutedEventArgs e)
{
var tableClient = CloudStorageContext.
Current.Resolver.
CreateCloudTableClient();

var tableName = "Student";

tableClient.CreateTableIfNotExist(
tableName,
p =>
{
var context = CloudStorageContext.
Current.
Resolver.
CreateTableServiceContext();
var sampleData = new Student
{  Grade = "A",
Name="DJ",
RollNumber="1",
PartitionKey="s2",
RowKey="s"
};

context.AddObject(tableName, sampleData);

context.BeginSaveChanges(
asyncResult =>
{
var response = context.EndSaveChanges(asyncResult);

},
null);
});

}
}
}


Once you run on click event of the button you should able to add row in the table. I hope this post is useful. Thanks for reading.

6 responses to “Windows Azure Storage Client Library for Windows Phone: Part 1”

  1. […] Read original post by Dhananjay Kumar at Debug Mode […]

  2. […] post is number 46 in his series and is on vibrating the device using the VibrateController classWindows Azure Storage Client Library for Windows Phone: Part 1Dhananjay Kumar discusses getting and getting started using the Azure Storage Client library in your […]

  3. Hi, this post put me on the right way to work with the library. However, I get a FormatException on the constructor of the StorageCredentialsAccountAndKey class. Looks like the account key string must be formatted. Do you know how to accomplish this? thanks in advance

  4. […] more about Windows Azure Storage Client Library for Windows Phone: Part 1 by @debug_mode here Related Articles:Video on How to work with AutoCompleteBox in Windows Phone 7 by @debug_modeBlock […]

  5. […] Windows Azure Storage Client Library for Windows Phone: Part 1 […]

  6. I am also getting FormatException.
    Alessandro, did you fix it?

Leave a comment

Create a website or blog at WordPress.com