How to get the number of licenses not assigned to a user using MicrosoftGraphAPI(C#)

南薗直弥 26 Reputation points
2023-01-10T08:31:02.197+00:00

Using MicrosoftGraph, is there a way to get the number of licenses not assigned to a user? "the number of licenses not assigned to a user" correspond to the red frame in the image below.

The Microsoft 365 admin center 277808-image.png

I would like to get the number of BusinessBasic licenses.

Microsoft Security | Microsoft Graph
0 comments No comments
{count} vote

Accepted answer
  1. Vasil Michev 119.7K Reputation points MVP Volunteer Moderator
    2023-01-10T08:43:48.637+00:00

    While not directly exposed, you can calculate this from the information exposed as part of the https://graph.microsoft.com/v1.0/subscribedSkus query. The prepaidUnits will give you the total number of seats available for a given SKU, whereas the consumedUnits gives you the currently assigned ones. Substract the numbers to get the number of unassigned seats.

    GET https://graph.microsoft.com/v1.0/subscribedSkus
        {
        "capabilityStatus": "Enabled",
        "consumedUnits": 5,
        "skuId": "87bbbc60-4754-4998-8c88-227dca264858",
        "skuPartNumber": "POWERAPPS_INDIVIDUAL_USER",
        "appliesTo": "User",
        "prepaidUnits": {
            "enabled": 10000,
            "suspended": 0,
            "warning": 0
        }
    

1 additional answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,376 Reputation points
    2023-01-10T11:22:28.047+00:00

    Hi @南薗直弥

    @Vasil Michev gave an idea, if you are using graph C# SDK, refer to my code:

    using Azure.Identity; 
    using Microsoft.Graph;    
    
    
    try
    {
          var scopes = new[] { "https://graph.microsoft.com/.default" };
    
          var tenantId = "tenant id";
    
          // Values from app registration
          var clientId = "client id";
          var clientSecret = "client secret";
    
          // using Azure.Identity;
          var options = new TokenCredentialOptions
          {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
          };
    
          // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
          var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret, options);
    
    
          GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential,scopes);
    
          var subscribedSkus = await graphClient.SubscribedSkus
                .Request()
                .GetAsync();
    
          for (int i = 0; i < subscribedSkus.Count; i++) {
    
          var sub = subscribedSkus[i].PrepaidUnits.Enabled - subscribedSkus[i].ConsumedUnits;
    
                if (sub != null) {
    
                      Console.WriteLine(subscribedSkus[i].SkuPartNumber + ":" + sub);   
                }              
          }          
    }
    catch (Exception ex) { 
    
          Console.WriteLine(ex);
    }
    

    ![277866-image.png][1]


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.