none
Getting 400 Bad Request when trying to add a user to an AD group using Microsoft Graph RRS feed

  • Question

  • I am dealing with the extremely strange issue. The code that I built worked just fine one year ago, but now it doesn't.

    I am trying to add a user to an AD group but get 400 bad request error. What can I do to fix it? Should I switch to a certain version for ActiveDirectory package?

    Thank you in advance. Here is my function

    public static async Task<string> AddGroupMember(string accessToken, string groupId, string memberId)
    {
        var status = string.Empty;
        try
        {
            string endpoint = "https://graph.microsoft.com/v1.0/groups/" + groupId + "/members/$ref";
            string queryParameter = "";
    
            // pass body data 
            var keyOdataId = "@odata.id";
            var valueODataId = "https://graph.microsoft.com/v1.0/directoryObjects/" + memberId;
    
            var values = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>(keyOdataId, valueODataId)
            };
            var jsonData = $@"{{ ""{keyOdataId}"": ""{valueODataId}"" }}";
            var body = new StringContent(jsonData, Encoding.UTF8, "application/json");
    
    
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint + queryParameter))
                {
                    request.Content = body;
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.StatusCode == HttpStatusCode.NoContent)
                            status = "Member added to Group";
                        else
                            status = $"Unable to add Member to Group: {response.StatusCode}";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            status = $"Error adding Member to Group: {ex.Message}";
        }
    
        return status;
    }


    Tuesday, October 29, 2019 5:10 PM

Answers

  • Hello James,

    I tested code snippet in my side, and it's working to add member to group if the member not existed in the Group, please see the capture below:

    Then I tested to run the same code snippet, repeat adding the member to the group, it will throw Bad Request.

    Seems the issue will happen when repeating adding existed member to group, please check this point in your side to see if you have added duplicated members to the group.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, October 30, 2019 6:31 AM

All replies

  • Hello James,

    I tested code snippet in my side, and it's working to add member to group if the member not existed in the Group, please see the capture below:

    Then I tested to run the same code snippet, repeat adding the member to the group, it will throw Bad Request.

    Seems the issue will happen when repeating adding existed member to group, please check this point in your side to see if you have added duplicated members to the group.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, October 30, 2019 6:31 AM
  • Thank you very much!!! It was exactly the case!!!
    Wednesday, October 30, 2019 5:09 PM