Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to Use Onedrive Features in a Windows Phone 8 Application

, 18 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This tips show you how to get a windows Phone 8 which interacts with OneDrive.

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)
        {
        //the session status is connected so we need to set this session status to client
        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();//deletes the file if it already exists
                    if (myIsolatedStorage.FileExists(fileName))
                    {
                        myIsolatedStorage.DeleteFile(fileName);
                    }//now we use a StreamWriter to write inputBox.Text to the file and save it to IsolatedStorage
                    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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hadrich Mohamed
Student
Tunisia Tunisia
Software Engineering student in the International Institute of technology of Sfax (IIT),Microsoft Certified Professional : Specialist in C#,Microsoft Student Partner , IEEE IIT SB member, IIT Microsoft Club President And an Appli Academy 2013-2014 Certified.
Mail : hadrichmed@gmail.com
Phone : +216 27 61 61 40
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 18 Apr 2014
Article Copyright 2014 by Hadrich Mohamed
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid