Add/delete members to M365 groups using MS Graph API with Golang

Viewed 130

I'm trying to add and/or delete group members using graph/api with Golang

I am able to list members, or owners for groups, but not able to add to delete members.

//---CODE--- To list member of groups this code works an intended

cred, err := azidentity.NewClientSecretCredential("7c...","16...","sj...", nil,)
auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"https://graph.microsoft.com/.default"})
requestAdapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
result, err := graphClient.Users().Get()
// get member in 1 group
graphClientGroup := msgraphsdk.NewGraphServiceClient(requestAdapter)
r2, err := graphClientGroup.GroupsById("...group-id...").Members().Get()

When I try any variation for adding including as listed in MS docs: https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=go

requestBody := msgraphsdk.New()
requestBody.SetAdditionalData(map[string]interface{}{
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/{id}",
}
graphClient.GroupsById(&groupId).MembersById(&directoryObjectId).Post(requestBody)

This code will not compile, other variations will run but not produce new group members or return an error.

For deleting members in M365 groups I tried to run code without success such as:

graphClient.GroupsById("groupId...").MembersById("memberID...").Ref().Delete()

No errors are returned and NO member deleted from group?

Thank you, for any guidance.

2 Answers

As mentioned in the documentation in GitHub:

The Microsoft Graph Go SDK is currently in Community Preview and breaking changes are expecting to happen to the SDK based on community's feedback.

The error with post() has been already reported. Details here

This issue is now fixed, as of 2022-05-31 v0.25.0

Related