Search code examples
azuremicrosoft-graph-api

Looking up users through Graph API fails with "reference-property objects are not present"


I am trying to look up users through Graph API version 6.5.0 with the following code:

graphClient.users().byUserId("userId@thedomain.dk").get()

but my lookup fails with

com.microsoft.graph.models.odataerrors.ODataError: Resource 'userId@thedomain.dk' does not exist or one of its queried reference-property objects are not present.

I can create calendar events in the users outlook calender using code like this:

graphClient.users()
            .byUserId("userId@thedomain.dk")
            .calendar()
            .events()
            .post(theEvent);

I am guessing that it is the "one of its queried reference-property objects are not present"-part that is the issue. I have no clue what that means though.

I have the following permissions set on the application client I am using:

  • Application.Read.All
  • Calendars.Read
  • Calendars.ReadWrite
  • User.Read
  • User.Read.All

Solution

  • The error "Resource 'xxx@xxx.xxx' does not exist or one of its queried reference-property objects are not present" usually occurs if the user you are querying is not present in the Microsoft Entra ID tenant.

    enter image description here

    • As you stated you are able to create the calendar events by passing the UPN of the user it might be the issue that you are not passing valid UPN for the user API call.

    In my environment, I created a Microsoft Entra ID application and granted User.Read.All API permission which is required to call user API.

    enter image description here

    I used the below code and able to successfully get user details using Microsoft Graph API version 6.5.0:

    package com.example;
    
    import com.azure.identity.ClientSecretCredential;
    import com.azure.identity.ClientSecretCredentialBuilder;
    import com.microsoft.graph.models.User;
    import com.microsoft.graph.serviceclient.GraphServiceClient;
    
    public class GraphApiExample {
    
        public static void main(String[] args) {
    
            final String clientId = "ClientID";
            final String tenantId = "TenantID";
            final String clientSecret = "ClientSecret";
            final String userId = "ruk@XXX.onmicrosoft.com";
    
            final String[] scopes = new String[] { "https://graph.microsoft.com/.default" };
    
            final ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                    .clientId(clientId)
                    .tenantId(tenantId)
                    .clientSecret(clientSecret)
                    .build();
    
            if (null == scopes || null == credential) {
                throw new RuntimeException("Unexpected error");
            }
            final GraphServiceClient graphClient = new GraphServiceClient(credential, scopes);
    
            try {
                // Get the user by userId (email or userPrincipalName)
                User user = graphClient.users().byUserId(userId).get();
    
                System.out.println("User ID: " + user.getId());
                System.out.println("User Display Name: " + user.getDisplayName());
    
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    }
    

    enter image description here

    If still the issue persists, check the below:

    • Make sure that you are passing the User Principal Name of the user:

    enter image description here

    • Or try to pass the Object ID of the user to query instead of UPN.
    • Ensure that the user exists and is correctly configured in the Azure AD as If the user doesn't exist in Azure Active Directory, you will get this error. You can double-check if the user exists.
    • Double-check the user’s existence using Microsoft Graph Explorer.
    • Check if you are querying a user by email not by UPN instead.
    • If the user belongs to another tenant, then configure the application and multitenant and try.

    Otherwise, try to call UserCollectionResponse result = graphClient.users().get(); API which lists all users in the tenant (instead of waiting for the customer) and check if the user you are querying exists. This API will work as you have User.Read.All API permission granted to the application.