Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,510 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
//other-imports
)
scopes := []string{"https://graph.microsoft.com/.default"}
graphClient := msgraphsdk.NewClientSecretCredential(cred, scopes)
requestParameters := &graphgroups.ItemMembersRequestBuilderGetQueryParameters{
Count: &requestCount,
Orderby: []string{"displayName"},
Top: &requestTop,
Select: []string{"id", "displayName", "userType", "appId", "mail", "onPremisesSyncEnabled", "deviceId"},
}
configuration := &graphgroups.ItemMembersRequestBuilderGetRequestConfiguration{
Headers: headers,
QueryParameters: requestParameters,
}
members, err := graphClient.Groups().ByGroupId("group-id").Members().Get(context.Background(), configuration)
for _, member := range members.GetValue() {
user, ok := member.(*msgraphmodel.User)
info := map[string]interface{}{
"id": user.GetId(),
"displayName": user.GetDisplayName(),
"userType": user.GetUserType(),
"mail": user.GetMail(),
}
}
# current response
[
{
"displayName": null,
"id": "1",
"mail": null,
"userType": null
},
{
"displayName": null,
"id": "2",
"mail": null,
"userType": null
}
]
There is no mail in member backingStore. Therefore mail is null. How can I get group member like http response? The expect response is :
{
"@odata.context": "xxx",
"@odata.count": xx,
"@odata.nextLink": "xxx",
"value": [
{
"@odata.type": "#microsoft.graph.user",
"id": "1",
"displayName": "aa",
"userType": "Member",
"mail": "******@email.com",
"onPremisesSyncEnabled": true
},
{
"@odata.type": "#microsoft.graph.user",
"id": "2",
"displayName": "bb",
"userType": "Member",
"mail": "******@email.com",
"onPremisesSyncEnabled": true
},
...
]
Hi @TC
Try the following code snippet:
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphgroups "github.com/microsoftgraph/msgraph-sdk-go/groups"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestParameters := &graphgroups.GroupItemMembersRequestBuilderGetQueryParameters{
Select: [] string {"id","displayName","userPrincipalName","userType","mail","onPremisesSyncEnabled"},
}
configuration := &graphgroups.GroupItemMembersRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
}
members, err := graphClient.Groups().ByGroupId("group-id").Members().Get(context.Background(), configuration)
Before doing this, make sure the calling app has Directory.Read.All
permissions.
Hope this helps.
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.