Microsoft Graph opens an extensive approach for developers to connect various services across Microsoft Cloud. A Single endpoint requests the Microsoft Cloud to enable the possibility of connecting with various resources in the cloud.

Microsoft Graph requests the endpoint with the resources with properties enabled to get the required values for the requested users. By default, Microsoft Graph’s user resource doesn’t provide a direct way to get the license information for the user.

But, Office 365 Admin Center has the following post request endpoint to retrieve the users and their license information.

POST https://admin.microsoft.com/admin/api/Users/ListUsers

We do not have access to the above endpoint to use it in a custom application. Instead of the admin API, we can use Microsoft Graph as an alternative for retrieving the Users and Subscription information.

After researching the Microsoft Graph’s Users resource of v1.0 version, we can conclude that it doesn’t have the license information with the user data. But the beta version of Users resource has the property assignedLicenses with an array of Subscription Ids.

GET https://graph.microsoft.com/beta/users

Programmatically Access Office 365 User Licenses Using Microsoft Graph

In the future, it may get added to the public version under the Users resource.

The above endpoint returns the only ID of the subscribed licenses. To get the name of each subscription id, we have another resource called “SubscribedSkus”. This endpoint returns the available subscriptions with a name and Id with other properties of each subscription.

Get https://graph.microsoft.com/v1.0/subscribedSkus

These two endpoints return the values separately. So, we will create a client application to merge the above two endpoint requests and display it in a table format.

Instead of developing the application from scratch, we will download the starter code from “Angular Graph Rest Starter (Sample)” app. This starter comes with a code for authentication, bootstrap for responsive and Angular js to control the data and HTML.

This setup requires a Node.js environment.

Prepare the Application

Methods to request Microsoft Graph API

Get all users method to send the request to beta endpoint of Microsoft Graph to fetch all users information.

  1. getlicenses: function getlicenses(){
  2. return $http.get('https://graph.microsoft.com/v1.0/subscribedSkus?$select=SKUPartNumber,skuId');
  3. }

The getlicenses method requests the subscibedSKus resource and only retries the Subscription name and Id.

Methods to render fetched values

We have the methods to request the Microsoft Graph. Now, we have to prepare the code for calling those methods and render the value in the HTML.

Vm.getAllUsers method calls the getallusers method from graphHelper file and store to the retrieved users array value in allusers property.

  1. vm.licences ={};
  2. vm.getUserswithLicenses = function(){
  3. GraphHelper.getlicenses().then(function(response){
  4. var licResponse = response.data.value;
  5. for (var i = 0; i < licResponse.length; i++) {
  6. vm.licences[licResponse[i].skuId] = licResponse[i];
  7. }
  8. vm.getAllUsers();
  9. });
  10. }

vm.getUserswithLicenses method first stores the retrieved subscription information in licenses property and within the success calls the getAllUsers method to retrieve the users. We will Angular js feature to update the users’s skuid to subscription name.