Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file to Onedrive programmatically from C#?

I want to create a doc, docx, pptx or excel file from C# direct to my Onedrive account. I have try this but it's not working for me. Anybody have any idea what I did wrong ? Thanks

public async Task<ActionResult> CreateWordFile()
{
   LiveLoginResult loginStatus = await authClient.InitializeWebSessionAsync(HttpContext);
   if (loginStatus.Status == LiveConnectSessionStatus.Connected)
   {
        var fileData = new Dictionary<string, object>();
        fileData.Add("name", "Document.docx");
        fileData.Add("Content-Type", "multipart/form-data; boundary=A300x");
        fileData.Add("type", "file");

        LiveOperationResult getResult = await connectedClient.PostAsync("me/skydrive/files", fileData);
    }

    return View();
}

EDITED: The error that I get is this one:

"The header 'Content-Type' is missing the required parameter: 'boundary'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: Microsoft.Live.LiveConnectException: The header 'Content-Type' is missing the required parameter: 'boundary'."

like image 467
cozmin-calin Avatar asked Dec 19 '14 11:12

cozmin-calin


1 Answers

A couple of things:

  1. The dictionary provided to PostAsync only populates fields in the request body, and so adding Content-Type in there doesn't have any effect
  2. File resources are mean to be created using the UploadAsync method, which requires content. I don't believe there's an API you can call to tell the service to create a blank Office document.
like image 115
Brad Avatar answered Sep 23 '22 03:09

Brad