简体   繁体   中英

Microsoft Graph API: get user organization

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": "jdoe@mycomp.net",
            "id": "c8f63ba1-5150-44c1-b456-468040f12345"
        }
  ]
}

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

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.

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM