How can I make msgraph-go-sdk graphClient result look like http response?

TC 0 Reputation points
2024-03-22T07:32:47.89+00:00
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": "aa@email.com",
            "onPremisesSyncEnabled": true
        },
        {
            "@odata.type": "#microsoft.graph.user",
            "id": "2",
            "displayName": "bb",
            "userType": "Member",
            "mail": "bb@email.com",
            "onPremisesSyncEnabled": true
        },
        ...
]
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,532 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 36,811 Reputation points
    2024-03-25T07:29:35.9866667+00:00

    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.