Search code examples
c#windows-runtimeuwponedrive

How to upload a folder/file on onedrive using the onedrive sdk for windows 10 UWP


I'm trying to use the onedrive sdk to upload content with this link.

Edit:

  • I have permission to write files into the onedrive.
  • I'm able to login and view the folders of the onedrive.
  • Also the app is associated with the store.

Code:

using (contentStream)
{
    var uploadedItem = await oneDriveClient
                                 .Drive
                                 .Root
                                 .ItemWithPath("path/to/file.txt")
                                 .Content
                                 .Request()
                                 .PutAsync<Item>(contentStream);
}

what exactly is path/to/file.txt and what is contentStream? Can someone give me a clear example as I'm getting a 'Microsoft.OneDrive.Sdk.OneDriveException'.


Solution

  • One of the sections of the documentation you might want to take a look at is the authentication process as this will get your started in using the OneDrive SDK in your UWP application.

    To break down what's going on in the example code they've posted for you, the contentStream is the file you're wanting to uploading in the form of a stream. If you're using a StorageFile object to upload to OneDrive, you'll need to call the OpenStreamForReadAsync method on the storage file to get the contentStream which is present in the sample.

    The "path/to/file.txt" section is where you'd like to store the file within the OneDrive root folder. So if you add a folder structure here, you'll put it in there, for example, if you're uploading image.jpg, you might have the following /AppId/Images/image.jpg.