Introduction
Microsoft provides a Windows Live Connect SDK
which allows you to easily connect, upload and download files from and to OneDrive (previously called Skydrive).
Background
Before starting, we have to download the
latest Live SDK for Windows, Windows Phone 8, and .NET from http://msdn.microsoft.com/en-us/live/ff621310.aspx.
Start Creating Your Own Windows Phone 8 App
After downloading the version 5.5, we are
going to create a new Windows Phone 8
application called « SkyDrive Example » in which the user can
register using his Microsoft Live Account in order to send the file to Skydrive.
Left click on «References under your project
in the Solution Explorer Windows and add these essential references to your project:
Change the title of our application to be more
significant and add «SignInButton » to Toolbox to make the
authentification process.
So, you will find many components, so Write
« SignInButton » in the filter zone to find this functionality as
shown below:
Well done. Now, we want to create a Client Id
in order to store the user’s authorization information to not sign in every time
the app opens.
Go to https://account.live.com/developers/applications/create and we will enter the name of our application and accept the term of
uses.
After doing that, you will find your own client ID as
shown in the picture below:
Now, you need to add this code into the
MainPage.xaml in order the add the signInButton
and don’t forget to add your
own client ID in Client Id Zone:
<StackPanel x:Name="TitlePanel"
Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="SkyDrive Example"
Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="Upload a File" Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Controls:SignInButton Name="loginButton"
Content="SignInButton" HorizontalAlignment="Left"
Margin="110,75,0,0" VerticalAlignment="Top"
Scopes="wl.signin wl.offline_access wl.skydrive_update"
SessionChanged="loginButton_SessionChanged" Visibility="Visible"/>
<Button Content="Upload" HorizontalAlignment="Left"
Margin="110,256,0,0" VerticalAlignment="Top"
Width="211" Click="Upload_Click"/>
</Grid>
Now, it is time for managing our MainPage.cs.
So that we don’t want to register every time the application launches,
we are going to add the code below starting with adding the essential
libraries:
using Microsoft.Live;
using Microsoft.Live.Controls;
Don’t forget to declare the LiveConnectClient
.
private LiveConnectClient client;
Add the following code for
the
loginButton_SessionChanged
helper method.
private void loginButton_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
if (e != null && e.Status == LiveConnectSessionStatus.Connected)
{
this.client = new LiveConnectClient(e.Session);
}
else
{
this.client = null;
}
}
If the user clicks on download, he will send a
file called ‘sample.txt’ that contains ‘hello world’ to his Onedrive Account.
private async void Upload_Click(object sender, RoutedEventArgs e)
{
if (client != null)
{
try
{
string fileName = "sample.txt";
IsolatedStorageFile myIsolatedStorage =
IsolatedStorageFile.GetUserStoreForApplication(); if (myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
} using (StreamWriter writeFile = new StreamWriter
(new IsolatedStorageFileStream(fileName, FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
writeFile.WriteLine("Hello world");
writeFile.Close();
}
IsolatedStorageFileStream isfs = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read);
var res = await client.UploadAsync("me/skydrive", fileName, isfs, OverwriteOption.Overwrite);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
else
{
MessageBox.Show("Please sign in with your Microsoft Account.");
}
}
Conclusion
Well done! We have seen together how to
implement the OneDrive API in your Windows Phone 8 application and how to send
a simple file to the OneDrive.