Monday, September 26, 2011

Live SkyDrive API: Get List of Folder's Files

The new Live Connect SkyDrive API, which was released in prelimary form at Build 2011, finally delivers an "official" way to manipulate files and folders on SkyDrive. For being a preview release, the docs look reasonably complete.

However, one thing that wasn't clear to me (from the docs) was how to iterate through the contents of a folder while, for example, searching for a particular file.'s a bare-bones code snippet that shows how to search the root folder of the signed-in user's SkyDrive for the file "test.txt".

private void FindFileTestDotTxt()
    client.GetCompleted += 
        new EventHandler<LiveOperationCompletedEventArgs>(client_GetCompleted);

void client_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
    Dictionary<string, object> resultDict = 
        (Dictionary<string, object>) e.Result;

    List<object> items = (List<object>)resultDict["data"];

    foreach (object o in items)
        Dictionary<string, object> item = (Dictionary<string, object>)o;

        if (item["name"].ToString() == "test.txt")
            // found it 
            Console.WriteLine("File URL: {0}", item["source"].ToString());

No comments: