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:
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.
In my environment, I created a Microsoft Entra ID application and granted User.Read.All
API permission which is required to call user API.
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());
}
}
}
If still the issue persists, check the below:
UPN
instead.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.