Category Archives: OneDrive

Adding OneDrive support to an application

I wanted to create an application which stores data for synchronisation between devices, so figured the cheapest way might be to use one of the free storage mediums, in this case OneDrive.

  • Create an application, mine’s a WPF app.
  • Add the NuGet package Microsoft.OneDriveSDK
  • Add the NuGet package Microsoft.OneDriveSdk.Authentication

Note: Windows allows the storage of data to the user’s roaming profile to be stored in the “cloud”. So if your application is solely Windows based this might be a better solution than using OneDrive directly.

Registering our application

Before we get into any coding we’ll need to register our application with the Microsoft Application Registration Portal, instructions can be found here.

Basically you’ll be prompted to log into the Microsoft Application Registration Portal, then you should Add your application, supplying it with a name. Once completed, this will supply us with an application id and application secrets.

As my sample application is a native application then I don’t need to create an app password.

Just click Add Platform and add the platforms you are wanting to support (again mine’s a simple native application).

Writing some code

First off we’ll need to authenticate the user, so we use an authentication adapter. Here’s my code

var msaAuthenticationProvider = new MsaAuthenticationProvider(
   AppId,
   "https://login.live.com/oauth20_desktop.srf",
   new[] { "onedrive.readwrite", "offline_access" });

When you run this code, you’ll be presented with a login screen requiring username/email and password, then a dialog to tell you your application is requesting permissions to access OnDrive and asking you to grant or deny them.

Authorisation scopes, such as onedrive.readonly can be found here.

Now we need to authenticate and access the one drive client.

await msaAuthenticationProvider.AuthenticateUserAsync();

var oneDriveClient = new OneDriveClient(
   "https://api.onedrive.com/v1.0", 
   msaAuthenticationProvider);

Once we get an instance of the OneDriveClient we can interact with the One Drive files/file system. Here’s an example snippet of code which gets to a know folder location, for example imagine we have a folder named Public/MyApp then we could use

var item = await 
   oneDriveClient
      .Drive
      .Root
      .ItemWithPath("Public/MyApp")
      .Request()
      .GetAsync();

We can also use a “special folder” which the application data is stored to by changing the permission scope to onedrive.appfolder, for example

var msaAuthenticationProvider = new MsaAuthenticationProvider(
   AppId,
   "https://login.live.com/oauth20_desktop.srf",
   new[] { "onedrive.appfolder", "offline_access");


await msaAuthenticationProvider.AuthenticateUserAsync();

var item = await oneDriveClient
   .Drive
   .Special
   .AppRoot
   .Request()
   .GetAsync();

Further Reading

OneDrive Dev Center
Official projects and SDKs for Microsoft OneDrive
Using an App Folder to store user content without access to all files