Search code examples
azureazure-active-directoryazureportalazure-rest-api

Azure search for user, groups, or service principle by name or email address via Rest API or python module (MS Graph)?


In azure when adding a user, group, or service principle to a role you can search by name and email address in the same search (screenshot below). When I look at the MS Graph APIs there are separate APIs for users, groups, and service principles (MS Graph API documentation links below). And it looks like the search options cannot be mixed (just display name or just email).

Does anyone know how to achieve a search like this using an Azure REST API. I'm curious if anyone knows what calls Azure is actually doing and if they are part of the published rest API? Or if one search is combining like multiple API calls which would be confusing since are paginated that would be hard to figure out what to display from which....

I'm building an app to add permissions and I'm trying to recreate a feature like this search.

Only way I can think of to achieve this now would be to select an option to search for either 'groups', 'users', or 'service principles'. Then another options to select search by 'email' or search by 'displayName' (but not both as the same search). This seems more clunky but technically ok.... but I'd rather do it like the azure screenshot below.

https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http

https://learn.microsoft.com/en-us/graph/api/group-list?view=graph-rest-1.0&tabs=http

https://learn.microsoft.com/en-us/graph/api/serviceprincipal-list?view=graph-rest-1.0&tabs=http

Azure Portal Screenshot


Solution

  • Microsoft Graph API provides batching functionality where you can batch multiple requests together and send them as a single request for processing. In your case, on the server-side (Graph API side) three requests will be processed but from your application you will be sending a single request and get a single response.

    Your request would be something like:

    {
      "requests": [
        {
          "id": "1",
          "method": "GET",
          "url": "/users?$filter=<your-filter-criteria>"
        },
        {
          "id": "2",
          "method": "GET",
          "url": "/groups?$filter=<your-filter-criteria>"
        },
        {
          "id": "3",
          "method": "GET",
          "url": "/servicePrincipals?$filter=<your-filter-criteria>"
        }
      ]
    }
    

    You can learn more about the batching capability in Microsoft Graph API here: https://learn.microsoft.com/en-us/graph/json-batching.