Graph API retrieve multiple applications based on list of appids

62 Views Asked by At

I am currently trying to filter Azure applications based on a list of app IDs. I have the necessary setup in place, with the graph service client having "Application.ReadWrite.OwnedBy" permissions and the same client app is added as an owner to the applications I want to retrieve. I have the following code snippet that filters based on a single appId, but I want to modify it to filter based on the entire list of appIds.

Code snippet I am currently using to filter single appId details

var appIds = ["id1","id2","id3"]
var applications = await this.graphServiceClient.ApplicationsWithAppId(appIds[0]).GetAsync((requestConfiguration) => requestConfiguration.QueryParameters.Select = ["id", "appId", "displayName", "requiredResourceAccess"]);

I want to filter based on the entire appIds array

1

There are 1 best solutions below

1
Sridevi On BEST ANSWER

I registered one application and added "Application.ReadWrite.OwnedBy" permission of Application type to it as below:

enter image description here

Now, I added this as Owner to the applications that I want to retrieve:

enter image description here

To filter applications based on the entire appIds array, you can make use of below sample code:

using Azure.Identity;
using Microsoft.Graph;

class Program
{
    static async Task Main(string[] args)
    {
        var scopes = new[] { "https://graph.microsoft.com/.default" };
        var tenantId = "tenantId";
        var clientId = "appId";
        var clientSecret = "secret";
        var appIds = new[] { "appId1", "appId2", "appId3" };

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        try
        {
            var filterExpression = string.Join(" or ", appIds.Select(id => $"appId eq '{id}'"));

            var applications = await graphClient.Applications
                .GetAsync(requestConfiguration =>
                {
                    requestConfiguration.QueryParameters.Filter = filterExpression;
                    requestConfiguration.QueryParameters.Select = new string[] { "appId", "displayName" };
                });

            foreach (var application in applications.Value)
            {
                Console.WriteLine($"App Name: {application.DisplayName}");
                Console.WriteLine($"App ID: {application.AppId}");
                Console.WriteLine();
            }
        }
        catch (ServiceException serviceException)
        {
            Console.WriteLine(serviceException.Message);
        }
    }
}

Response:

enter image description here