Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Use Onedrive Features in a Windows Phone 8 Application

0.00/5 (No votes)
18 Apr 2014 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here