Programmatically Accessing The Office 365 User Licenses Using Microsoft Graph

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

  • Navigate to the Repo https://github.com/ktskumar/angular-graph-rest-starter

  • Download the source from GitHub and extract the zipped file.

  • Open the extracted folder in your favorite editor. I am using Visual Studio Code editing and managing the client-side source code.

  • Use App Registration Portal to generate the Client Id by creating a new app.

  • Navigate to public/scripts/config.json file. Then add the client Id and Redirect URI, which you have created in App Registration Portal.

    Programmatically Access Office 365 User Licenses Using Microsoft Graph

  • Replace the below line with graphScopes in config.json to delegate the application to access the resources.

    graphScopes: ["user.read user.read.all Directory.ReadWrite.All Directory.AccessAsUser.All"]
  • Then open the command terminal and run the command “npm install”. This will download and add the dependencies under the node_modules folder.

Methods to request Microsoft Graph API

  • Navigate to public/scripts/graphHelper.js.

  • Then add the below snippets, to fetch all the available users and all the subscriptions for the tenant.
    1. getallusers: function getallusers(){  
    2.      return $http.get('https://graph.microsoft.com/beta/users');  
    3.  }, 

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.

  • Navigate to public/controllers/maincontroller.js

  • Then add the below snippets,
    1. vm.allusers;  
    2. vm.getAllUsers = function(){        
    3.   GraphHelper.getallusers().then(function(response){  
    4.     vm.allusers = response.data.value;  
    5.   });  

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.

  • To change the header of the view, add the below line by replacing vm.view.
    1. vm.view = "All Users with License Status";  
  • We are sending the request on page load. So once we are authenticated, we have to the call the method vm.getUserswithLicenses. So within processAuth() method, add the below snippet under the line, emailAddress = user.mail || user.userPrincipalName; in two places.
    1. vm.getUserswithLicenses();  
  • Navigate to public/views/main.html file and search for <!-- ENTER HTML CODE HERE --> and replace below HTML snippet.
    1. <!--Show if there is no users available in the tenant -->  
    2. <div ng-show="main.allusers.length == 0" class="text-danger">No Users found!</div>  
    3. <!-- Render the table with users and subscription names-->  
    4. <table ng-show="main.allusers.length > 0" class="table table-hover">  
    5.   <thead class="thead-dark">  
    6.     <tr>  
    7.       <th scope="col">#</th>  
    8.       <th scope="col">Display Name</th>  
    9.       <th scope="col">Licences</th>  
    10.     </tr>  
    11.   </thead>  
    12.   <tbody>  
    13.     <tr ng-repeat="usr in main.allusers track by $index">  
    14.       <th scope="row">{{$index+1}}</th>  
    15.       <td>{{usr.displayName}}</td>  
    16.       <td>  
    17.         <span ng-repeat="lic in usr.assignedLicenses">  
    18.           {{main.licences[lic.skuId].skuPartNumber}},  
    19.         </span>  
    20.         <span ng-show="usr.assignedLicenses.length == 0">  
    21.           UNLICENSED  
    22.         </span>  
    23.       </td>  
    24.     </tr>  
    25.   </tbody>  
    26. </table>  
  • Now, run the “npm start” command from the terminal.

  • After successfully runs, the app asked you to log in. So click the connect button on the top right corner.

    Programmatically Access Office 365 User Licenses Using Microsoft Graph

  • In the opened popup, log in with your credentials. It redirects you to the page, where you have to click the Accept button to delegate your permission to the app.

  • After the approval, the output seems like below,

    Programmatically Access Office 365 User Licenses Using Microsoft Graph


Similar Articles