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

Windows Phone 8 - A Simple Helper Class for Working with SkyDrive Files using SkyDrive API

, 14 Oct 2013
Rate this:
Please Sign up or sign in to vote.
How to upload, download and synchronize application's files using SkyDrive

Contents

  • Introduction
  • Demo Application
  • Basic Concepts of using SkyDrive API in Windows Phone 8 projects
  • Setting Up your development environment and a project for using the helper class
  • Using SkyDriveFileHandler class
  • Conclusion

Introduction

One of the common tasks in mobile development is sharing some application files between user's devices or storing files outside device. There are many Cloud services such as Microsoft SkyDrive, DropBox, Google Disk, etc. that allow developers to use their APIs for these purposes. You may consider using a wide range of cloud services in your application and give users an opportunity to choose one to store and sync application files. This approach has some benefits and some disadvantages on the other hand. I believe if you create an application for multiple platforms (e.g. Android, Windows Phone, iOS...) you definitely should use several services for it - a user may have an account for one of them and doesn't have for others. In this situation, we probably should allow him to use the account he has and not make him register a new one although we have to support a larger codebase now.

But let's have a look at what we have in case of Windows Phone platform. Microsoft has its own cloud storage service - Microsoft SkyDrive. Every Windows Phone user doesn't have a need to register a new account for this service because he or she already has one!

You can find documentation on how to use SkyDrive API in your projects at MSDN. That said, it could be quite tricky to integrate interaction with SkyDrive in Windows Phone application. So I've created a simple helper class - SkyDriveFileHandler for working with files (uploading, downloading and synchronizing) using SkyDrive service.

In this article, I'm going to explain how to use it in your apps and some basic concepts of using SkyDrive API.

Demo Application

I've created a simple demo application that shows how to use SkyDriveFileHandler in a real project. You can download it by the link above. The application contains three buttons which have event handlers for uploading, downloading and synchronizing a file asynchronously.

If operation completes successfully, it shows MessageBox with text that all is fine. If user isn't logged in Microsoft Account, it shows account's login form.

Basic Concepts of using SkyDrive API in Windows Phone 8 projects

SkyDrive API provides developers an opportunity for working with files, folders, albums, photos, etc. using user's SkyDrive account. In order to use it in your application, you have to:

  1. Register your app in Live Connect Developer Center and get unique ID for your app
  2. Download LiveSDK and add its references to your project
  3. Create an instance of LiveConnectClient class and use its methods to communicate with SkyDrive service

And of course, you should have a configured phone emulator that has an established internet connection for debug purposes.

Setting Up Your Development Environment and a Project for Using the Helper Class

Please take the following steps to set up your project:

  1. Create a new Windows Phone 8 project
  2. Add references to Microsoft.Live.dll and Microsoft.Live.Controls.dll. These libraries provide a set of components and methods to communicate with SkyDrive service using SkyDrive API. In Visual Studio, you can use NuGet Package Manager in order to get Live SDK package which includes them.

  3. Add SkyDriveFileHandler class to your project.
  4. I highly recommend to download Windows Phone Power Tools that will allow you to browse IsolatedStorage of your phone or phone emulator using a GUI instead of having to step through the IsolatedStorage file browser on the command line. You can observe your application files located in IsolatedStorage and their properties to find out when files are synchronized with SkyDrive properly.

  5. To be able to use LiveSDK, you have to register your app in Live Connect Developer Center and get ClientID. You will use it when you create an instance of the helper class to connect to SkyDrive.

Using SkyDriveFileHandler Class

At first, don't forget to add references to Microsoft SkyDrive API:

using Microsoft.Live;
using Microsoft.Live.Controls;  

Then you should define a session variable or property which will be used to interact with SkyDrive. I prefer to do it in App.xaml.cs in order to make it accessible at every place of an app.

//App.xaml.cs
public static LiveConnectSession LiveSession { get; set; }

After we've created a session variable, let's create an object of our helper class in a phone page:

 private SkyDriveFileHandler skydrive; 

There's one very important thing you should know - you can create an instance of SkyDriveFileHandler only when application page has been loaded completely. Don't try to initialize it in a page constructor! Instead you can do it in "page loaded" event handler or in any other event handler for a GUI-element such as button, etc. The constructor of the class requires to specify name for a folder which will contain application's files as third parameter ("skydrivetestapp" in our case).

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
	//WARNING! Third parameter is YOUR application ID, which you get after registering
	//your app in SkyDrive
        skydrive = new SkyDriveFileHandler(App.LiveSession,
                                "your_application_id", "skydrivetestapp");
}   

Using the helper class is pretty simply. The class has three public members:

public async Task<OperationStatus> UploadFile(string fileName);
public async Task<OperationStatus> DownloadFile(string fileName);
public async Task<OperationStatus> SynchronizeFile(string fileName); 

I think it is obvious what they are for. UploadFile gets a file's name and tries to find it in IsolatedStorage and upload it to SkyDrive folder with the exact same name. DownloadFile downloads file asynchronously from SkyDrive and saves it in IsolatedStorage.

SynchronizeFile tries to find a file by given filename in local storage and in SkyDrive folder. Then it compares dates when both files were changed and replaces an old file with the newer one. If file hasn't been found in IsolatedStorage, the method downloads it from SkyDrive and vice versa - if the file exists in IsolatedStorage but doesn't exist in SkyDrive folder, the method uploads it to SkyDrive.

These methods return a variable of OperationStatus enum type. You can check it to know if an operation has been completed successfully. So let's have a look at how you can use the methods in your app:

private async void btnUpload_Click(object sender, RoutedEventArgs e)
{
     if (await skydrive.UploadFile(fileName) == OperationStatus.Completed)
            MessageBox.Show("The file has been uploaded!");
}
 
private async void SynchronizeBtn_Click(object sender, RoutedEventArgs e)
{
     if (await skydrive.SynchronizeFile(fileName) == OperationStatus.Completed)
         MessageBox.Show("The file has been synchronized successfully!");
}
        
private async void DownloadBtn_Click(object sender, RoutedEventArgs e)
{
    if (await skydrive.DownloadFile(fileName) == OperationStatus.Completed)
         MessageBox.Show("The file has been downloaded to IsolatedStorage!");
} 

Don't forget to mark your methods as "async" - all methods work asynchronously in the background in order not to block application user interface.

Conclusion

You can use SkyDriveFileHandler class "as-is" or change and modify it on your own. Actually, it's a pretty crude sketch at this moment. For example, you can add additional methods you need and modify current methods for better control of exception handling, checking free space in IsolatedStorage and so on. As for me, I'm going to make a class library for working with SkyDrive later.

Thanks for your attention!

License

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

About the Author

Dennis Shchuka
Software Developer
Russian Federation Russian Federation
I've been started to program in Delphi and Visual C++ 6 about 10 years ago.
I've built a lot of corporate applications and intranet portals using LAMP software bundle as well as Microsoft technologies since 2007. An experienced database architect. I'm also intrested in mobile apps development as hobbyist.
Now I'm specializing in software development using .NET. I'm a big fan of C# and .NET.
 
e-mail me at: sybinfotech[at]gmail.com

Comments and Discussions

 
QuestionCan't download files Pinmemberdamianom13-Jan-14 10:02 
AnswerRe: Can't download files PinmemberDennis Shchuka14-Jan-14 2:40 
GeneralRe: Can't download files Pinmemberdamianom14-Jan-14 2:46 
QuestionGreat PinmemberMember 1046204912-Dec-13 0:29 
AnswerRe: Great PinmemberDennis Shchuka14-Jan-14 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 14 Oct 2013
Article Copyright 2013 by Dennis Shchuka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid