×
Namespaces

Variants
Actions
Revision as of 08:36, 30 November 2012 by hamishwillee (Talk | contribs)

SkyDrive - How to upload content on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to upload files to SkyDrive cloud storage from Windows Phone, using Live Connect

WP Metro Icon File.png
WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code Example
Source file: Noise Reduction (uses this code)
Tested with
Devices(s): Nokia Lumia 800
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Article
Keywords: SkyDrive
Created: galazzo (06 Sep 2012)
Last edited: hamishwillee (30 Nov 2012)

Contents

Introduction

SkyDrive is a Microsoft cloud service that allows you to access your files and documents on all your phones and computers, and to share them easily with your friends and colleagues. The service is available as an extension to the native file browser on many platforms (e.g. Windows, Mac, iOS, Android) and is also accessible from most web browsers. In addition, it is possible to access the service directly in your apps using the set of controls and APIs in the Live Connect SDK (these allow developers to integrate with SkyDrive, Hotmail, and Messenger).

Skydrive-logo-200.png

This article explains how to obtain the LiveConnect SDK, add the APIs to your project, login to the service, and upload a file.

Prerequisites

At the time of writing this article, the LiveConnect APIs are not included by default on the Windows Phone SDK.

Before you start developing:


Creating the application

Create a new HelloWorldPhone Windows Phone application in Microsoft Visual Studio 2010 Express for Windows Phone using the Visual C# -> Silverlight for Windows Phone -> Windows Phone Application project template and name it SkyDrive:

SkyDrive-step-000.png

Choose last OS version

SkyDrive-step-001.png

Bring up the Add Reference dialog and go to the .Net tab. Scroll down and add the Microsoft.Live and Microsoft.Live.Controls assemblies to your project.

SkyDrive-step-002.png

Open MainPage.xaml in the designer. Bring the Toolbox in view and right-click to select Choose Items… Under Windows Phone Components tab, find the SignInButton control in the Microsoft.Live.Control namespace and add to your toolbox (You only need to do this once).

SkyDrive-step-003.png

If everything is well installed you should see the SignInButton control in your toolbox. Drag it onto your designer surface.

SkyDrive-step-004.png

Scopes and permissions

Before your app makes requests of the Live Connect APIs to work with Live Connect info, in most cases you must get permission from the user to access that info or to create new objects on behalf of the user. In the Live Connect APIs, this permission is called a scope. Each scope grants a different permission level.

There are three types of scopes:

  • Core scopes are central to the Live Connect APIs and involve users' core profile and contact data.
  • Extended scopes allow you to work with users' extended profile and contact data.
  • Developer scopes allow you to work with developers' client IDs.

For our sample project we need the following scopes

Core scopes

Scope Enables
wl.basic Read access to a user's basic profile info. Also enables read access to a user's list of contacts.
wl.offline_access The ability of an app to read and update a user's info at any time. Without this scope, an app can access the user's info only while the user is signed in to Live Connect and is using your app.
wl.signin Single sign-in behavior. With single sign-in, users who are already signed in to Live Connect are also signed in to your website.

Extended scopes

Scope Enables
wl.skydrive_update Read and write access to a user's files stored in SkyDrive.

Into .xaml file your code should look like that:

<my:SignInButton Content="Button" Name="skydrive" Scopes="wl.basic wl.signin wl.offline_access wl.skydrive_update" SessionChanged="skydrive_SessionChanged"  />

ClientId

Now we need to connect our application to Live Connect. Go to the application management site and click the Create Application. Create an application and provide a name for it.

SkyDrive-step-005.png

If everything goes well you will be provided with a ClientID and a Client secret code:

SkyDrive-step-006.png

Now let's add the ClientID to our SignInButton code:

<my:SignInButton Content="Button" Name="skydrive" ClientId="00000000440xxxxx" Scopes="wl.basic wl.signin wl.offline_access wl.skydrive_update" Branding="Skydrive" TextType="SignIn" SessionChanged="skydrive_SessionChanged"  />

Using SkyDrive APIs

using Microsoft.Live;
using Microsoft.Live.Controls;
 
// SkyDrive session
private LiveConnectClient client;

SkyDrive Login session management

private void skydrive_SessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
{
if (e.Session != null && e.Status == LiveConnectSessionStatus.Connected)
{
client = new LiveConnectClient(e.Session);
MessageBar.Text = "Signed in.";
client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(signin_GetCompleted);
client.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(uploadClient_UploadCompleted);
client.GetAsync("me", null);
}
else
{
MessageBar.Text = "Not signed in:" + e.Status.ToString();
client = null;
}
}
 
void signin_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBar.Text = "Hello, signed-in user!";
}
else
{
MessageBar.Text = "Error calling API: " + e.Error.ToString();
}
}

Here the login screenshot

Login screenshot

Uploading a file

private void uploadButton_Click(object sender, EventArgs e)
{
if (client != null)
{
MessageBar.Text = "Uploading to SkyDrive";
IsolatedStorageFileStream fileStream = null;
 
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
fileStream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
client.UploadAsync("me/SkyDrive", strSaveName, fileStream, OverwriteOption.Overwrite);
}
else
{
MessageBox.Show("You must be signed for SkyDrive");
}
}
 
void uploadClient_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBar.Text = "File uploaded with success!";
MessageBox.Show("File uploaded with success!");
}
else
{
MessageBar.Text = "File uploading failed!";
MessageBox.Show("File uploading failed!");
}
}

Code sample

1012 page views in the last 30 days.