4

I am using Microsoft Graph API to get some details of the user. I also use AD for Authentication and Authorization.

In this application, user, after login, will search for some user(s) and needs some details for all the users matching the search.

I hit below users api, with filters, but I am not getting any details regarding company name for matching user(s).

https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'jo')

Below is the response for the same

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
  "@odata.nextLink": "https://graph.microsoft.com/v1.0/users?$skiptoken=X%2744537074020001000000203A636F6D7061735F766A61407465737473636F7270696F67726F75702E6E657429557365725F33386664353661362D366361612D343939332D393264642D383439633938613039393033B900000000000000000000%27",
  "value": [
        {
            "businessPhones": [],
            "displayName": "John Doe",
            "givenName": "John",
            "jobTitle": null,
            "mail": null,
            "mobilePhone": null,
            "officeLocation": null,
            "preferredLanguage": null,
            "surname": "Doe",
            "userPrincipalName": "[email protected]",
            "id": "c8f63ba1-5150-44c1-b456-468040f12345"
        }
  ]
}

What I need to do to get the company name for the users of my organization?

6
  • By organization, do you mean Administrative Unit or users in a separate tenant? Commented Apr 9, 2019 at 15:27
  • i mean company name field for each user. I used to get it in old graph api using users. But now i am not able to get it. Commented Apr 10, 2019 at 6:28
  • 1
    @ForamkumarParekh Oh, if you just want to get companyName, you can use beta graph api. You will get it by calling graph.microsoft.com/beta/users{userid} You can see the response in my answer. Commented Apr 10, 2019 at 7:55
  • @Tony, for suggesting to use beta, but that is what i was trying to avoid, as beta many have some changes and depending on those changes, i might again have to update the code. So I was hoping to get the solution in V1.0. Anyways thanks. Beta is fine. You can post it as answer and I will accept it. Commented Apr 10, 2019 at 11:29
  • @ForamkumarParekh Thank you. I have added this to the answer. Besides, you can find the graph changelog here learn.microsoft.com/en-us/graph/… Commented Apr 10, 2019 at 11:48

2 Answers 2

3

The user resource returns only a subset of properties by default. Per the documentation:

Note: Getting a user returns a default set of properties only (businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName). Use $select to get the other properties and relationships for the user object.

In other words, you need to add a $select parameter to your query that lists the properties you want to be returned. For example, if you want to retrieve id, userPrincipalName, and companyName, you would use:

https://graph.microsoft.com/v1.0/users?$select=displayName,id,jobTitle,companyName

The complete set of available properties can be found in the User Resource Type docs.

Sign up to request clarification or add additional context in comments.

2 Comments

makes sense and eliminates the beta endpoint consideration discussed earlier.. only minor difference I notice is that for some reason https://graph.microsoft.com/beta/users returns a lot of properties including companyName while v1 returns only default set unless you explicitly select..
If you add $whatif=true to the query params, you'll notice the difference between Beta and v1.0 is that v1.0 adds a $select by default while beta does not.
2

I want this details for all the users from just one login

This is not possible. The only way to get organization details is using https://graph.microsoft.com/v1.0/organization api.

You need to provide the access token to call this api. In your application, you login with one user to get the access token.

If you use common in the token request url, you will get the default organization for that login user.

If you use specific tenant in the request url, you will get the organization details for that tenant.

If you just want to get the companyName of the user, you can call https://graph.microsoft.com/beta/users/{userid} to get it.

enter image description here

5 Comments

I have more than 1 organization. I want to know which user falls under which organization. So this answer does not help.
@ForamkumarParekh If you use the graph explorer to call this api, you will get the default organization which used to create this user. If you use login.microsoftonline.com{tenantid}/oauth2/v2.0/token to get the access token, you will get the organization details regarding tenantid which you assigned.
@ForamkumarParekh You will be member only in one organization, and be Guest in other organization.
Thanks tony for the clarification. But I believe that even hitting organization api might provide the default organization of the logged in user. I want this details for all the users from just one login. I guess there is some mis interpretation of the question. Let me put it to you in this way, I am creating application, in which a user, after login, can access the details of other users. I need the organization details for the users in the list
the knowledge you shared was helpful, but it did not help me get what I needed. Sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.