Question

I need to backup data from my WP7 app to Skydrive, this file is xml file. I know how to connect to skydrive and how to create folder on skydrive:

try
{
    var folderData = new Dictionary<string, object>();
    folderData.Add("name", "Smart GTD Data");

    LiveConnectClient liveClient = new LiveConnectClient(mySession);
    liveClient.PostAsync("me/skydrive", folderData);
}
catch (LiveConnectException exception)
{
    MessageBox.Show("Error creating folder: " + exception.Message);
}

but I don't know how to copy file from isolated storage to skydrive.

How can I do it?

Was it helpful?

Solution

That's easy, you can use the liveClient.UploadAsync method

private void uploadFile(LiveConnectClient liveClient, Stream stream, string folderId, string fileName) {
    liveClient.UploadCompleted += onLiveClientUploadCompleted;
    liveClient.UploadAsync(folderId, fileName, stream, OverwriteOption.Overwrite);
}

private void onLiveClientUploadCompleted(object sender, LiveOperationCompletedEventArgs args) {
    ((LiveConnectClient)sender).UploadCompleted -= onLiveClientUploadCompleted;
    // notify someone perhaps
    // todo: dispose stream
}

You can get a stream from IsolatedStorage and send it like this

public void sendFile(LiveConnectClient liveClient, string fileName, string folderID) {
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
        Stream stream = storage.OpenFile(filepath, FileMode.Open);
        uploadFile(liveClient, stream, folderID, fileName);
    }
}

Note that you need to use the folder ID when uploading the stream. Since you are creating the folder, you can get this ID when the creation of the folder is complete. Simply register for the PostCompleted event when posting the request for folderData.

Here's an example

private bool hasCheckedExistingFolder = false;
private string storedFolderID;

public void CreateFolder() {
    LiveConnectClient liveClient = new LiveConnectClient(session);
    // note that you should send a "liveClient.GetAsync("me/skydrive/files");" 
    // request to fetch the id of the folder if it already exists
    if (hasCheckedExistingFolder) {
      sendFile(liveClient, fileName, storedFolderID);
      return;
    }
    Dictionary<string, object> folderData = new Dictionary<string, object>();
    folderData.Add("name", "Smart GTD Data");
    liveClient.PostCompleted += onCreateFolderCompleted;
    liveClient.PostAsync("me/skydrive", folderData);
}

private void onCreateFolderCompleted(object sender, LiveOperationCompletedEventArgs e) {
    if (e.Result == null) {
        if (e.Error != null) {
          onError(e.Error);
        }
        return;
    }
    hasCheckedExistingFolder = true;
    // this is the ID of the created folder
    storedFolderID = (string)e.Result["id"];
    LiveConnectClient liveClient = (LiveConnectClient)sender;
    sendFile(liveClient, fileName, storedFolderID);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top