Recently, I got the chance to play around a little with the new OneDrive API (REST). Boy is it cool, but only after an inital phase of crawling through the web resources filled with old tutorials and code snippets of old API parts… The documentation really isn’t as good as I hoped and I had some troubles finding the right starting point. That’s why I wanted to quickly summarize my first steps here:
What was my task?
For a small medical health app (running on Windows Phone 8.1) I am currently working on, we needed a way to easily export all the stored information to OneDrive (as a backup and to run certain kinds of analyses on the way more powerful MS Excel, compared to the rudimentary Excel on the phone). Further, we wanted to have the opportunity to import the exported files (in XML format) to the app, in case some data is lost (due to a corrupt update of the app). So, a single file-export and a file-import is all we needed.
Where to start?
I needed almost an hour until I knew where to start. Most resources Google showed me were concerning old OneDrive projects, and had little to do with the new shiny API. And the official documentation lacked many details. Finally, I found out what to do. Here is how:
- You need to register your app in the Windows Store.
- Get the SDK from Nuget (just search for “Live SDK” and install)
- Associate your App with the store (registered under 1.). In Visual Studio, right click on Project > Store > Associate App with the Store and select a unique Name.
- Add the Capability to the project (in the project manifest) to access the internet.
- Here you find out how you can authenticate with the OneDrive API. What I really appreciate is the Single Sign-On feature that you get with Windows Runtime apps. This basically means that you don’t need to worry anymore about any log-in forms, persisting the log-in or something similar. The Windows device handles this itself. At the first start, it asks for permission (depending on the requests you ask permission for (login, read, write, etc.)
- Now you are good to go:
- This site in the official documentation is useful to learn more about uploading or downloading files.
- For the download/import, I first also wrote a downloading function and realized that I need to give the user the opportunity to select the exact file before the download. After a quick websearch I found out that I don’t need to create my own FilePicker, but that Windows Runtime apps have a FileOpenPicker (hmm, why have I missed this?). This will also show the OneDrive app (if installed) and then download the app. Very simple and clean. This StackOverflow post shows an example of how the FileOpenPicker works.
- For Android, there also exists a File Sharer and Picker
- Here you find many other (tiny) examples and excerpts of how you could use the API (with Windows Runtime apps)
I hope I can save you some time with the hints. If you have any questions, please let me know.
PS: Did you know, that Microsoft relies on exactly this OneDrive API for their own apps and services? Cool!
Additional Resources: