Over a million developers have joined DZone.

Things To Know About Uploading Files to SkyDrive from Windows Phone

· Mobile Zone

Over this weekend I had the opportunity to work on an application that uses the Microsoft Live SDK to upload data files to SkyDrive. I must admit that I really like the way the SDK was built and it provides the necessary harness to get a developer started with minimal effort. There are a couple of things worth mentioning, though, and new developers might not be aware of those when building their applications.

1. You can't upload files with the MP3 extension through the SDK

The existing filter won't let you get those music files up in the cloud. If you take a look at this list, you will notice that only WAV files are allowed to be uploaded. Attempting to upload a MP3 file will result in an exception. The workaround for this situation is fairly easy - all you have to do is change the extension to WAV or simply add a ".wav" suffix to the name of the file that is being uploaded. Once the file is up, it can be played by Windows Media Player as a regular MP3 audio file even though the extension does not match.

2. You need offline access for persistent sign-in

While using the SDK, the developer will most likely insert a SignInButton component somewhere in the application. As a part of the setup, the developer has to also specify the scopes for the application that is registered to use the API. Here is the thing - users don't want to constantly sign-in each time the application starts. Instead, sign-in once and continue using the Live functionality until an explicit sign-out command is issued. To do this, the developer needs to declare the following scope: wl.offline_access. Don't worry, the user will be notified that the application needs to access data even while the user is offline. What this gives is the ability to auto-login whenever the application is relaunched.

3. Upload from IsolatedStorage - simple

All you need to do is this:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
             IsolatedStorageFileStream isfs = isf.OpenFile(fullName, FileMode.Open, FileAccess.Read);
             LiveClient.UploadAsync("me/skydrive, fileName + ".wav", isfs);
             isfs.Close();
}

The LiveClient class is an instance of LiveConnectClient.

4. "This is not the path you are looking for"

Let's say that you want to upload the file to a SkyDrive folder and not in the root. Do you think something like this will work?

LiveClient.UploadAsync("me/skydrive/folderName, fileName + ".wav", isfs);

The answer is no. You will need to list the existing SkyDrive folders and get the associated metadata before you can do something like that. The code to do this might look similar to this (where skyDriveFolder has the default value of me/skydrive):

LiveClient.GetCompleted += (g_s, g_eva) =>
                        {
                            if (g_eva.Error == null)
                            {
                                Dictionary<string, object> folderData = (Dictionary<string, object>)g_eva.Result;
                                List<object> folders = (List<object>)folderData["data"];

                                foreach (object item in folders)
                                {
                                    Dictionary<string, object> folder = (Dictionary<string, object>)item;
                                    if (folder["name"].ToString() == "Beem")
                                        skyDriveFolder = folder["id"].ToString();
                                }

                                LiveClient.UploadAsync(skyDriveFolder, fileName + ".wav", isfs);
                            }
                        };
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}