How to GET all files from SharePoint REST API with Postman

We’ll be using Postman to demonstrate how to get SharePoint files via the API.

SharePoint is a collaboration and document management platform that many enterprise organizations use to organize, share, and access files. There are many use cases for the SharePoint REST API, but the one of the most common requests we see is how to GET all files from the API.

{{blog-cta-100+}}

Getting Files from SharePoint

Preparation

In order to access SharePoint’s API, you’ll first need a sandbox SharePoint account and an OAuth application. The main way to get both of these is to apply for a Microsoft Developer account here.

You may be prompted to select the products you are interested in. If available, select the options related to SharePoint, OneDrive, and Microsoft Graph.

Your sandbox account will renew every 90 days if you actively use it.

You’ll need to populate your SharePoint sandbox account with data. To do this:

1. Within your SharePoint account, create a site. (Either a Team or Communication site will work.)

2. Once your new site is created, navigate to Documents, and click + New to add new files and folders!

To create an OAuth app, navigate to your developer account https://portal.azure.com/#home, then navigate to Azure Active Directory > App Registrations > + New Registration to create a new OAuth app. If you’re using Postman to test the integration, add the Postman URL as your redirect URI (https://oauth.pstmn.io/v1/browser-callback).

You’ll see an app created, with a provided client ID.

The last step is to create a client secret. To do so, click Client credentials > Client secrets > + New client secret.

Now you’re fully set up!

Related: How to access your API key in SharePoint

SharePoint API authentication

Oauth:

authorize URL: https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize

token URL: https://login.microsoftonline.com/organizations/oauth2/v2.0/token

scopes: “offline_access https://graph.microsoft.com/.default

Copy and paste your client id from your OAuth app’s “Application (client) ID” and your client secret from your OAuth app’s “Client Secret > Value”.

In Postman, once you’ve entered all the params, click “Get Access Token”. This will take you through the OAuth flow, and at the end, you should be able see an “Authentication complete” screen. Use the access token provided by this flow, which Postman will automatically add as auth headers for your subsequent API requests!

Getting files & folders from SharePoint

1. First, access your sites, using the following command:

GET https://graph.microsoft.com/v1.0/sites?search=*

SharePoint is organized by sites, and sites have their own files & folders. The “search=*” query param allows you to pull all sites.

2. Second, choose a site to pull drives from. Using the site’s id, you can then make the following command to get the drives. Usually, there is one drive per site.

GET https://graph.microsoft.com/v1.0/sites/:site/drives

3. Lastly, you’ll want to get the <code class="blog_inline-code">DriveItems</code> under these Drives. <code class="blog_inline-code">DriveItems</code> is how Microsoft represents its files and folders. Using the <code class="blog_inline-code">drive id</code> and <code class="blog_inline-code">site id</code>, call the following to get the drive’s files and folders.

GET https://graph.microsoft.com/v1.0/sites/:site/drives/:drive/items/root/children

Additionally, you can use the select query param to view only the fields you want to see.

4. The above endpoint only surfaces the <code class="blog_inline-code">DriveItems</code> in the root directory of the specified drive. To pull deeper items within a <code class="blog_inline-code">DriveItems</code> that is a folder, use the <code class="blog_inline-code">DriveItems</code> id and call the following.

GET https://graph.microsoft.com/v1.0/sites/:site/drives/:drive/items/:folder_id/children

Building a production-level integration

There are a couple of points to consider when turning this simple proof of concept into a production-level integration:

  1. You’ll need to implement logic to recurse through the folders, as well as identifying which <code class="blog_inline-code">DriveItems</code> are folders.
  2. You’ll need to implement token-based pagination, as SharePoint uses <code class="blog_inline-code">$skipToken</code> to paginate its response. This is especially important so that you don’t miss any data.
  3. If you need to do periodic syncs, you’ll also need to implement periodic polling of the SharePoint APIs.
  4. You’ll need to implement logic to handle the SharePoint OAuth flow, including providing a redirect URI and a bit of front-end support.

Conclusion

This article showed you how to GET all files from SharePoint.

As your needs for integrations grow, Merge connects your product to hundreds of integrations with one single API in HR, recruiting, accounting, ticketing, CRM and marketing automation. As with all of Merge’s Unified APIs, one API connection is enough to offer your users integrations to all File Storage platforms including Sharepoint, OneDrive, Dropbox, Box, Google Drive, and more.

You can schedule a demo with one Merge's integration experts to learn more.