How to combine two queries in Microsoft Graph API to get one dataset?

Mwinia 1 Reputation point
2022-08-29T15:40:47.23+00:00

I'm quite new to Microsoft Graph API, so I hope someone can help me with the following:

I'm currently working on a PowerBi dashboard where I want to visualize Azure AD Devices data, which I'm currently getting from the Microsoft Graph API. For getting the right dataset, I would like to combine two Graph datasets/queries. I would like to see all device information including the registeredOwner, in one query. With the two queries below I can find the correct data, but is there a possibility to merge these queries into one so that I can immediately see the registeredOwner of each Device?

GET https://graph.microsoft.com/v1.0/devices
GET https://graph.microsoft.com/v1.0/devices/{id}/registeredOwners

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,062 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Sheena-MSFT 1,731 Reputation points
    2022-08-29T16:06:47.16+00:00

    Hi @Mwinia ,

    Using batching we can combine multiple requests in one HTTP call.

    POST https://graph.microsoft.com/v1.0/$batch  
    Accept: application/json  
    Content-Type: application/json  
      
    {  
      "requests": [  
        {  
          "id": "1",  
          "method": "GET",  
          "url": "/devices  
        },  
        {  
          "id": "2",  
          "method": "GET",  
          "url": "/devices/id/registeredOwners"  
        }  
            
          
      ]  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


  2. CarlZhao-MSFT 42,031 Reputation points
    2022-08-30T09:17:13.903+00:00

    Hi @Mwinia

    Depending on your context, batching shouldn't work for you. Although batch processing can handle multiple requests, each request is independent of each other, and you want to call the /devices endpoint to get the id of each device, and then get the owner of each device according to the id, which shows that the two API endpoints are not independent of each other but are inherited.

    You can only meet this requirement using code, first calling the /devices endpoint and then traversal through the result set of that endpoint to get the ids of all devices, then passing the ids of the devices to the /devices/{id}/registeredOwners endpoint. I have written the complete code for you, please refer to:

    using Azure.Identity;  
    using Microsoft.Graph;  
    using Newtonsoft.Json;  
      
      
    var scopes = new[] { "https://graph.microsoft.com/.default" };  
      
    // Multi-tenant apps can use "common",  
    // single-tenant apps must use the tenant ID from the Azure portal  
    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);  
      
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);  
      
    var devices = await graphClient.Devices  
        .Request()  
        .GetAsync();  
      
    for (int i = 0; i < devices.Count; i++) {   
      
        var deviceId = devices[i].Id.ToString();  
      
        var registeredOwners = await graphClient.Devices[deviceId].RegisteredOwners  
        .Request()  
        .GetAsync();  
      
        Console.WriteLine("registeredOwners:" + JsonConvert.SerializeObject(registeredOwners));  
    }  
      
    Console.ReadLine();  
    

    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.


  3. CarlZhao-MSFT 42,031 Reputation points
    2022-08-30T09:39:33.723+00:00

    Hi @Mwinia

    Fortunately, I just found an easier way where you can directly expand the owner of each device using the $expand query parameter.

    GET https://graph.microsoft.com/v1.0/devices?$expand=registeredOwners  
    

    236102-image.png


    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.