Access SharePoint Data using Postman

In this series of posts, I will be explaining a couple of ways to access SharePoint data using Postman.

  1. Using an Azure AD app via Graph API (this post)
  2. Using the SharePoint App Registration

These approaches will allow us to test the APIs using Postman before we can use them in development.

In both approaches we first start by creating an app and then use the credentials of that app to get the access token. Once we get the access token we use that in the HTTP request to get the required data.

This post will cover accessing SharePoint data via Graph API.


Azure AD app

To register an app in Azure AD follow the normal steps. You can find various blogs that explain how to register an app one of them by Microsoft is here.

Add required application permissions to the app. In this case I will be providing the “ Create, edit, and delete items and lists in all site collections” permissions to the app under Microsoft Graph API.

Azure AD App Permissions

After that create a key for the app using the steps mentioned in earlier article. Copy the “Client Id” and the “Key” into a notepad as we need these later.

Azure AD App credentials

Also copy the directory id from the properties into a notepad as we need this later.

Directory Id

Those are the steps from the Azure side. Now to Postman.

Postman

One of the first things I like to do in Postman is creating an environment. An environment can be thought of as a container of variables that can be used in all the requests.

To create an environment click on the “cog” in the top right corner to open the “Manage Environments” window and then click on “Add”. Provide a relevant name for the environment and then add the following variables

Environment in Postman

Access Token Request

Now that the environment is set up, it’s time to send a POST request to get the token. Create a new request in Postman, name it as “Get Access Token For Graph” and change it’s request type to “POST”.

The URL will be

https://login.microsoftonline.com/{{directoryId}}/oauth2/v2.0/token

{{directoryId}} is an environment variable. So when we send the request {{directoryId}} will be replaced with the value we specified earlier.

Click on the “Body” tab of the request and add the following Key Value pairs

Body for getting access token using graph

Now click on “Tests” tab in the request and add the following javascript.

var json = JSON.parse(responseBody);
postman.setEnvironmentVariable("azureApp_bearerToken", json.access_token);

This code runs after the request is made. It extracts the “access token” from the response, creates an environment variable called “azureApp_bearerToken” and assigns it’s value to the retrieved access token.

Storing the access token in the environment

The request is now composed, save it and click on “Send”. This will provide the json response which has access token in it.

Sending the request

This should have created a variable called “azureApp_bearerToken” in the environment and assigned the value of it to the retrieved token. You can verify that by looking at the environment variables.

Site Id and List Id

Now that we have the token we can start making requests to SharePoint using the Microsoft graph endpoints. In this case we will be composing a request to display the title of the list items in a SharePoint site. For this we need the id of the SharePoint site and the id of the list where the items reside. To get the id of the site, head over graph explorer and sign in.

Run the following query

https://graph.microsoft.com/v1.0/sites/{host-name}:/{server-relative-path}

where “host-name” is your tenant name.sharepoint.com and “server-relative-path” is the relative path of the required site (usually begins with “/sites/”)

Example query

Running this query will return a json information of the site which has the “id” of the site as shown below. (format — “hostname,GUID”). Copy this id in a notepad.

To get the id of the list, run the following query

https://graph.microsoft.com/v1.0/sites/{site-id}/lists

where {site-id} id copied in the previous step.

Running this query will return all the lists in the site. Copy the id of the required list and save it. The id will be a GUID. A sample list response is shown below

List Id from Graph

Getting list items

Now that we have the site id and list id, let’s create a couple more postman environment variables to store those.

We are now ready to make the request to get the list items. To do that create a new GET request in Postman with the name “Get List Items Using Graph”. The URL will be

https://graph.microsoft.com/v1.0/sites/{{siteId_ModernTeam}}/lists/{{listId_TestList}}/items?$Select=Id&$expand=fields($select=Title)

This should return all the items in the list with their title.

To specify the access token for the request, click on the “Headers” tab and add the following

Get list items request with token

The request is now composed. Save it and click send. This will return a json response (similar to the one shown below) which will have the list item details.

If there is an error related to token, then please run the token request once again and then re-send the list item request.
List Item Response

You can modify the query in the request as per your need to get other data. This completes accessing SharePoint data via Graph using Postman.