4
votes

I have an Asp.Net website which uses Azure Active directory authorization (Using ADAL) and it returns basic attributes such as display name of an user. What I want is to GET custom attributes (For an e.g. employeeID) using the Microsoft Graph API (NOT Azure AD API)

Example Graph API query :

string graphRequest = string.Format(CultureInfo.InvariantCulture,
    "{0}{1}/users?api-version={2}&$filter=startswith(userPrincipalName, '{3}')",
    graphApiEndpoint,
    tenant,
    graphApiVersion,
    "SearchText.Text");
1
What does custom attributes mean? If you want to get different property set, you can use the OData $select query parameter.$select=employeeID. Or do you mean Azure B2c AD user custom attributes? - Tom Sun - MSFT
Yes, It's B2C Active Directory custom attributes. - Nirosh Sns

1 Answers

5
votes

You're calling Microsoft Graph using the URI pattern from Azure Graph API. These are distinct APIs that have different calling patterns.

The correct URI should be:

https://graph.microsoft.com/v1.0/users?$filter=startsWith(userPrincipalName, 'string')

By default, /users only returns a limited set of properties. From the documentation:

By default, only a limited set of properties are returned ( businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName ).

To return an alternative property set, you must specify the desired set of user properties using the OData $select query parameter. For example, to return displayName, givenName, and postalCode, you would use the add the following to your query $select=displayName,givenName,postalCode

Note: Certain properties cannot be returned within a user collection. The following properties are only supported when retrieving an single user: aboutMe, birthday, hireDate, interests, mySite, pastProjects, preferredName, responsibilities, schools, skills, mailboxSettings

In other words, if you need something other than the default properties, you need to explisitly request the property set you want.

Based on the above, you're example code should look something like this:

const string graphApiEndpoint = "https://graph.microsoft.com";
const string graphApiVersion = "v1.0";
const string userProperties = "id,userPrincipalName,displayName,jobTitle,mail,employeeId"
string graphRequest = string.Format(CultureInfo.InvariantCulture,
    "{0}/{1}/users?$select={2}&$filter=startswith(userPrincipalName, '{3}')",
    graphApiEndpoint,
    graphApiVersion,
    userProperties,
    "SearchText.Text");