Home > C#, Windows Phone, WinRT > How to organize application files on SkyDrive with Live SDK

How to organize application files on SkyDrive with Live SDK

24/06/2013

Often we utilize SkyDrive to store files that are used by our apps, as backup or to make them available across all our devices: thanks to the Live SDK, adding this feature is straightforward.

When you choose to use SkyDrive, we need to decide in which position to save our files. It’s not a good practice to save them in the root, because the number of files in this location would rapidly increase. We can then think about creating a directory for each app. However, as before, if all apps do this way, our SkyDrive would ultimately contain a long list of directories, making its structure confusing. So, a possible solution is to create a folder structure like the following:

Folders structure on SkyDrive

Folders structure on SkyDrive

It is a structure that resembles the one used by Windows. We have a ProgramData folder that has a subfolder for each platform (in this example, Windows 8 & Windows Phone). Under each of them, there is a directory with the Publisher Name and, finally, a folder with the app name, that contains application specific data.

If all apps follow this suggestion, SkyDrive accounts would be organized and clean.

So, let’s see how to do this in a Windows Store app. To create a directory on SkyDrive, we must use the PostAsync method of the LiveConnectClient class. However, the Live SDK doesn’t offer a method to verify if a folder already exists (and then to obtain a reference to it, that is needed to manage it). So, we need to iterate through all the folders and check for their name.

We can define an extension method to simplify the procedure:

public async static Task<string> CreateDirectoryAsync(this LiveConnectClient client, 
    string folderName, string parentFolder)
{
    string folderId = null;

    // Retrieves all the directories.
    var queryFolder = parentFolder + "/files?filter=folders,albums";
    var opResult = await client.GetAsync(queryFolder);
    dynamic result = opResult.Result;

    foreach (dynamic folder in result.data)
    {
        // Checks if current folder has the passed name.
        if (folder.name.ToLowerInvariant() == folderName.ToLowerInvariant())
        {
            folderId = folder.id;
            break;
        }
    }

    if (folderId == null)
    {
        // Directory hasn't been found, so creates it using the PostAsync method.
        var folderData = new Dictionary<string, object>();
        folderData.Add("name", folderName);
        opResult = await client.PostAsync(parentFolder, folderData);
        result = opResult.Result;

        // Retrieves the id of the created folder.
        folderId = result.id;
    }

    return folderId;
}

In the CreateDirectoryAsync method, we retrieve all the folders contained in parentFolder: using the GetAsync method with parameter /files?filter=folders,albums, we obtain both directories and albums (i.e., directories with images). If we find one with name equals to folderName, we retrieve its id and assing it to the folderId return value. Otherwise, it means that the folder doesn’t exist, so we create it with the PostAsync method and then retrieve the id that has been assigned to it.

Using this method is straightforward. For example, to create the SkyNote directory, as shown in the previous image, we need the following code:

// client is a variable of type LiveConnectClient.
var programDataDirId = await client.CreateDirectoryAsync("ProgramData", "me/skydrive");
var windows8DirId = await client.CreateDirectoryAsync("Windows 8", programDataDirId);
var publisherDirId = await client.CreateDirectoryAsync("Marco Minerva", windows8DirId);
var appDirId = await client.CreateDirectoryAsync("SkyNote", publisherDirId);

To refer to the root folder of SkyDrive, we use the constant me/skdrive. Then, to create a folder inside a certain directory, we must specifiy the Id of the parent (that has been retrieved by the previous invocation of CreateDirectoryAsync). The first time we execute the code, all the folders are created. At the end, the appDirId variable will contain the Id of the folder where our app can store and save its data.

Note that, as the user can delete files and folders from SkyDrive at any time, we should check for the existance of this directory structure every time we need to access files inside it.

Categories: C#, Windows Phone, WinRT
  1. 06/08/2013 at 16:59

    Thank you for this. Explained much better than MSDN🙂

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: