SkyDrive - How to upload content on Windows Phone
This article explains how to upload files to SkyDrive cloud storage from Windows Phone, using Live Connect
Article Metadata
Code Example
Tested with
Compatibility
Article
See Also
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).
This article explains how to obtain the LiveConnect SDK, add the APIs to your project, login to the service, and upload a file. The code works on Windows Phone 8. To use it on Windows Phone 7.x you need some extra jobs described here.
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:
- Download lastest Windows Phone SDK
- Download Live Connect SDK
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:
Choose last OS version
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.
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).
If everything is well installed you should see the SignInButton control in your toolbox. Drag it onto your designer surface.
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.
If everything goes well you will be provided with a ClientID and a Client secret code:
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, LiveConnectSessionChangedEventArgs e)
{
if (e != null && e.Status == LiveConnectSessionStatus.Connected)
{
this.client = new LiveConnectClient(e.Session);
this.GetAccountInformations();
}
else
{
this.client = null;
InfoText.Text = e.Error != null ? e.Error.ToString() : string.Empty;
}
}
private async void GetAccountInformations()
{
try
{
LiveOperationResult operationResult = await this.client.GetAsync("me");
var jsonResult = operationResult.Result as dynamic;
string firstName = jsonResult.first_name ?? string.Empty;
string lastName = jsonResult.last_name ?? string.Empty;
InfoText.Text = "Welcome " + firstName + " " + lastName;
}
catch (Exception e)
{
InfoText.Text = e.ToString();
}
}
Here the login screenshot
Uploading a file
First we create a Demo file to upload. Here an example of a bynary file:
private string fileName = "sample.dat";
private IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
private void CreateFileIntoIsolatedStorage()
{
if (isf.FileExists(fileName))
{
isf.DeleteFile(fileName);
}
IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(fileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
byte[] output = new byte[25];
for (int i = 0; i < 25; i++)
{
output[i] = (byte)(i);
}
isfStream.Write(output, 0, output.Length);
isfStream.Close();
}
In this example we call the method from constructor, but of course is possible to create the file when and where needed.
public MainPage()
{
InitializeComponent();
CreateFileIntoIsolatedStorage();
}
Now we can upload the file
private async void btnUpload_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = store.OpenFile(strSaveName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
try
{
LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive",
new Uri("/shared/transfers/"+fileName, UriKind.Relative),
OverwriteOption.Overwrite
);
InfoText.Text = "File "+fileName+" uploaded";
} catch (Exception ex) {
}
}
}
}
BackgroundUploadAsync
Parameters
- path
- Type: String
The folder ID of the folder in SkyDrive to upload the file to, for example "folder.8c8ce076ca27823f.8C8CE076CA27823F!142"
- uploadLocation
- Type: Uri
The unique isolated storage path to where the file is to be uploaded from, for example "/shared/transfers/myFile2.txt"
- option
- Type: OverwriteOption
Specifies whether the file that's being uploaded to SkyDrive should overwrite an existing file with the same file name in that location.






Contents
Roope - Deprecated events
Please note the events like LiveConnectClient.GetCompleted are deprecated and replaced by async/await -pattern:
http://msdn.microsoft.com/en-us/library/live/hh826550.aspx_Roope_ 09:59, 14 March 2013 (EET)
Hamishwillee - Roope - thanks!
Sebastiano plans to update this when he has time. If you wanted to do so you'd be welcome!hamishwillee 06:48, 18 March 2013 (EET)
Hamishwillee - Thanks Sebastiano - work on WP8?
Looks like galazzo has made an update! Thanks very much. OK for me to remove these comments now?
Can you also confirm whether this code works on Windows Phone 8 and if so, which devices tested against? then I could update the categories and metadata appropriately.
Regards
Hamishhamishwillee 01:22, 3 April 2013 (EEST)
Galazzo -
Hi Hamish,
with this updates the code work on WP8. To work on WP7 you need update some packages as described here : http://blogs.msdn.com/b/bclteam/archive/2012/10/22/using-async-await-without-net-framework-4-5.aspx
I made my lastest test on a Nokia Lumia 820.
I will improve the article with ne woptions as soon as possible.
Regards,
Sebastianogalazzo 02:26, 3 April 2013 (EEST)
Hamishwillee - Excellent
Hi Sebastiano
Looks like a thorough job - thank you. Testing on 820 is cool - I don't care the device as long as it is WP8 and we record it (as you did) in the articlemetadata.
Regards
Hhamishwillee 02:24, 4 April 2013 (EEST)
Skysoft - BackgroundUploadAsync parameters
Hi everyone, compliments to Galazzo for excellent job. Please could you explain the parameters of BackgroundUploadAsync? I've implemented the example but on skydrive folder I've found an empty file. Maybe I've missed something or there's something wrong. Thanx a lot
D.Skysoft 19:52, 21 April 2013 (EEST)
Galazzo - BackgroundUploadAsync parameters
Hello,
I've updated the article with a brief deepening on BackgroundUploadAsync's parameters. You can find full reference here. If you need more help you can send me or share your portion of code in order to understand better where is the problem.
Regards,
Sebastianogalazzo 16:14, 22 April 2013 (EEST)
Skysoft - Not works
Hi Galazzo and thanx for your answer. But it still doesn't work. Better.. I upload the file but is empty... According the parameters shown before, you have missed the parameter "inputStream", and I thik could be fileStream in your code. How can work your code? After try this, I've an error "No overload of method BackgroundUploadAsync accept 4 parameters"... what's happening???
The second problem that I have seen, is that I can't specify my folder for remote upload. It's not really a problem, but I would understand why.
Thanx a lot
D.Skysoft 23:52, 23 April 2013 (EEST)
Galazzo - Not works
Hi,
I'm sorry, it was a mistake. I wrote wrong reference, but now I've changed. The correct reference link is [1].
About your problem I'm not able to understand your problem without to have an eye on portion of code.
Can you share something ?
Regards,
Sebastianogalazzo 00:00, 24 April 2013 (EEST)
Skysoft - Done...
Hi Galazzo, thanx again for correction and patience. I've solved simply using UploadAsync instead of BackgroundUploadAsync. In UploadAsync I can specify directly the stream. Now it works. Thanx.
D:Skysoft 00:17, 25 April 2013 (EEST)
Hamishwillee - Sebastiano - might be worth adding a note about UploadAsync
Up to you!
Regards
Hhamishwillee 09:59, 25 April 2013 (EEST)