Reader Level:
ARTICLE

Windows Azure Storage Client Library For Windows Phone: Part 1

Posted by Dhananjay Kumar Articles | Windows Azure January 10, 2012
The Windows Azure Storage Client Library allows you to perform operations on Windows Azure storage from a Windows Phone.
  • 0
  • 0
  • 11401

The Windows Azure Storage Client Library allows you to perform operations on Windows Azure storage from a 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 the 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 a service and the service will use the 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

WinAcrClnt1.jpg

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

WinAcrClnt2.jpg

At the 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 the following command:

PM> Install-Package Phone.Storage

WinAcrClnt3.jpg

After successful installation of the Phone.Storage package, you should have the following references and file in the Solution Explorer.

WinAcrClnt4.jpg

StorageInitializer.cs is a very important class. In this class you can set whether you want to perform an 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. The following is the uncommented section in file StorageInitalizer.cs. You need to provide your Windows Azure Storage account name and key.

WinAcrClnt5.jpg

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 { getset; }
        public string RollNumber { getset; }
        public string Grade { getset; }
    }
}


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 be added.

The 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 the table client as below. Set the table name.

WinAcrClnt6.jpg

After resolving the table client, you need to create a table as below:

  WinAcrClnt7.jpg

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

WinAcrClnt8.jpg

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

MainPage.xaml.cs

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);
                });
 
        }
    }
}

When you run the application, upon the click event of the button you should be able to add a row in the table. I hope this article is useful. Thanks for reading.
 

COMMENT USING

Trending up