4

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Earlgray
  • 647
  • 1
  • 8
  • 31

1 Answers1

5

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);
}
Patrick
  • 17,669
  • 6
  • 70
  • 85
  • 1
    Great it works, but I have problem in specify path to skydrive if I use my/skydrive then everything works great but I have folder MyData on skydrive and this is place where I want to upload that file but if I use path my/skydrive/MyData then app crash. How can I specify path correct? – Earlgray Apr 10 '13 at 20:33
  • That's a good question, I forgot to mention it. It's not the folder name, it's the folder *id*. You have to fetch the folder information and extract the id from the result, and use that id when uploading. – Patrick Apr 10 '13 at 23:29
  • thak you, can I get folder Id when I create folder (as in code to create folder what I wrote in my fisrst post)? – Earlgray Apr 11 '13 at 10:43
  • @Earlgray: I updated my answer with an example. The answer is yes. – Patrick Apr 11 '13 at 11:24
  • there is another problem when I write to PostComplete event: string storedFolderId = (string)e.Result["data"]; MessageBox.Show(storedFolderId); then app crash.. – Earlgray Apr 11 '13 at 20:37
  • What exception do you get? – Patrick Apr 11 '13 at 21:08
  • it show exception on row string storedFolderId = (string)e.Result["data"]; - KeyNotFoundException was not unhandled – Earlgray Apr 12 '13 at 17:37
  • Hm, what keys are available? You can check that when debugging. – Patrick Apr 13 '13 at 01:26
  • Im not shure where is it so this is print screen of debug: http://img708.imageshack.us/img708/3897/scrtb.png – Earlgray Apr 13 '13 at 14:18
  • Check the keys in the entries property – Patrick Apr 13 '13 at 14:45
  • Ive got it correct version is e.Result["id"]. Thank you very much. Next I have last question ho can I override folder if folder with same name exist? – Earlgray Apr 13 '13 at 17:51
  • Oops, sorry about that. Why would you want to override a folder? Do you mean remove all files in the folder and then recreate it? I don't think your users would like that.. – Patrick Apr 13 '13 at 18:38
  • because there is problem in create folder code when folder with same name exist. I need to create (or get folder) and then overide its content winth new data – Earlgray Apr 17 '13 at 20:15
  • @Earlgray: That's why you need to call `liveClient.GetAsync("me/skydrive/files");`, as noted in the code comment, and extract the id from the folder with the same name. – Patrick Apr 18 '13 at 12:51