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:
- Register your app in Live Connect Developer Center and get unique ID for your app
- Download LiveSDK and add its references to your project
- 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:
- Create a new Windows Phone 8 project
- 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.
- Add
SkyDriveFileHandler
class to your project.
- 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.
- 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.
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)
{
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!