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

Brian 46 Reputation points
2022-05-20T16:39:26.463+00:00

I'm trying to add and 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.

//---START CODE--- This code works an intended to list members. I can list members or owners
cred, err := azidentity.NewClientSecretCredential("7c...","166...","sjp...", 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-abc...").Members().Get()
//===END CODE---

But when I try any variation for adding including as listed in docs:
https://learn.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.

Deleting a member in M365 group I tried to run code without success:
graphClient.GroupsById(groupId...).MembersById(memberID...).Ref().Delete()
No errors and not able to delete member from group?

Thank you, for any guidance.

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Srinivasa Rao Darna 6,761 Reputation points Microsoft External Staff
    2022-05-20T17:40:22.133+00:00

    Hi @Brian ,

    This appears to be an issue with Graph SDK Golang with POST method, please find GitHub SDK issue https://github.com/microsoftgraph/msgraph-sdk-go/issues/155.

    You can follow this github issue.

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.