Skip to main content

OneDrive: Developer Blog


30DaysMSGraph – Day 29 – Use case: Upload files to OneDrive

List of all posts in the #30DaysMSGraph series

-Today’s post written by Jeremy Kelley

In Day 28 we created a webhook notification for Microsoft Graph events.  Today we will interact with files stored in OneDrive.


For today’s sample we’ll be using Visual Studio Community edition.  It is recommended that you have finished the basic console application sample from Day 15.

To follow along with the sample you can either download the provided sample code for Day 29 [Add link to GitHub] or follow these setup instructions:

  1. Install Visual Studio Community Edition
  2. Create a new blank Universal Windows application
  3. Add the following NuGet packages
    • Microsoft.Graph
    • Microsoft.Identity.Client
  4. Add the following using declarations to your MainPage class:

  1. In MainPage.xaml.cs add the following code below the MainPage constructor:

  1. Replace YOURAPPIDHERE with the id of your application created in the basic console application example.

Working with files

Files are the backbone of many tasks you might want to do with OneDrive or SharePoint using the Microsoft Graph APIs.  When working with files the first thing to start with is always a Drive.  Drives can be a user’s OneDrive or a Document Library in a SharePoint site.

Accessing the files in OneDrive is as easy as accessing the /me/drive endpoint.  From that starting point you can enumerate the children, download content, upload new files, and even do things like convert content from one format to another.

In SharePoint you first need to address the site that the file is in.  You can do this in one of several ways.

List all drives in a site by Id:

Get the default drive in a site by Id:


You can also access sites by path if you don’t happen to know the Id.

List all drives in a site by path:

Get the default drive in a site by path:

In this post and the associated code sample we’re going to look at how to upload files to either the currently logged in user’s OneDrive for Business or the default document library in the root site collection of the signed in user’s tenant.

You can easily adapt this code to upload to other locations as you see fit.


Add required scopes

When working with files you can ask for one of several permissions.  To access only the user’s OneDrive ask for Files.Read or Files.ReadWrite.  To access or create files anywhere the user has access use Files.Read.All or Files.ReadWrite.All.

To enable our code to upload files to either OneDrive or SharePoint we’ll use Files.ReadWrite.All.

Since you didn’t assign the Files.ReadWrite.All permission in a previous day ensure that you assign that now to your Azure AD application.  Navigate to the Preview App Registrations experience in the Azure AD portal and assign the delegated permission for Files.ReadWrite.All under Microsoft Graph API.


Upload a small file

For files less than 4MB you can use a PUT to upload the file directly to the location you want.  To implement the upload method, add the code below to your MainPage class.

Upload a large file

To upload files larger than 4MB we need to use the CreateUploadSession pattern.  In this pattern we instruct the service to begin a session that we will then upload chunks of data to one at a time.  To implement the large upload pattern add the following method to your class:

Add an interface

Now we need to add some interface elements to our project to allow us to pick files and upload them.

In your MainPage.xaml file replace the default Grid element with the following markup:

In your code behind (MainPage.xaml.cs) file add the following code to connect the buttons to call our upload methods:

Beyond upload

Uploading files is just the beginning.  When you upload files to SharePoint you can combine file operations with metadata to fulfill scenarios tailored to your needs.  If you are working in large scale scenarios please check out our best practices for discovering files and detecting changes at scale.

Try It Out

Navigate to the 30DaysMSGraph-TryItOut repo. Clone the repo and configure the project in the Day 29 sub-folder using steps from the README or this post.

If you run into any issues while building or configuring the project, please create a new Issue on the repo.


Join us tomorrow as we wrap up the 30 Days of Microsoft Graph series with community resources and next steps suggestions in Day 30.