azuremicrosoft-graph-apimicrosoft-graph-sdks

Graph API throws error - "cannot be used like a method"


I have a code that was used from: https://github.com/microsoft/AzureProvisioningUsingFunctions/tree/main

  var getAllMessages = await graphClient.Users[UserPrincipalName].Chats
 .GetAllMessages()
 .GetAsync();

It shows a compiler error:

Non-invocable member 'ChatsRequestBuilder.GetAllMessages' cannot be used like a method. AzureProvisioning C:\Deeku\Personal\work\usermanagement\Code\AzureProvisioning.cs 728 Active


In addition to this, I have a code that is also not working:

  var signIns = await graphClient
    .AuditLogs
    .SignIns.GetAsync((requestConfiguration) =>
       { 
        requestConfiguration.QueryParameters.Filter = "UserPrincipalName eq '{UserPrincipalName}'  and isInteractive eq true and status/errorCode eq 0"; 
        requestConfiguration.QueryParameters.Orderby = new string[] { "createdDateTime" 
   }; 
 });
 var signInsD = signIns.Distinct();

The error:

'SignInCollectionResponse' does not contain a definition for 'Distinct' and no accessible extension method 'Distinct' accepting a first argument of type 'SignInCollectionResponse' could be found (are you missing a using directive or an assembly reference?) AzureProvisioning


Solution

  • To getAllMessages, modify the code like below:

    var result = await graphClient.Users["{user-id}"].Chats.GetAllMessages.GetAsGetAllMessagesGetResponseAsync((requestConfiguration) => { requestConfiguration.QueryParameters.Top = 2; });
    

    Refer this MsDoc

    The error is because you are trying to use the Distinct method on the SignInCollectionResponse object.

    Hence as a workaround modify the code like below:

    namespace GraphApiExample
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                var clientId = "ClientID";
                var tenantId = "TenantID";
                var clientSecret = "Secret";
    
                var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
                var graphClient = new GraphServiceClient(clientSecretCredential);
    
                var userPrincipalName = "rukuser@xxx.onmicrosoft.com";
    
                var result = await graphClient.AuditLogs.SignIns.GetAsync((requestConfiguration) =>
                {
                    requestConfiguration.QueryParameters.Filter = $"UserPrincipalName eq '{userPrincipalName}' and isInteractive eq true and status/errorCode eq 0";
                    requestConfiguration.QueryParameters.Orderby = new string[] { "createdDateTime" };
                });
    
                if (result?.Value != null)
                {
                    
                    var signInsList = result.Value.ToList();
    
                    // Apply Distinct based on some criteria, for example, by Id
                    var distinctSignIns = signInsList
                        .GroupBy(s => s.Id)  // Group by Id
                        .Select(g => g.First())  // Select the first entry from each group
                        .ToList();
    
                    foreach (var signIn in distinctSignIns)
                    {
                        Console.WriteLine($"Sign-in ID: {signIn.Id}, UPN: {signIn.UserPrincipalName}, Date: {signIn.CreatedDateTime}");
                    }
                }
                else
                {
                    Console.WriteLine("No sign-ins found.");
                }
            }
        }
    }
    

    enter image description here

    Reference: List signIns - Microsoft Graph v1.0 | Microsoft