Ajinkya G
Ajinkya G

Reputation: 23

Add Members to group in azure active directory using Graph API gives URl invalid error

I am trying to add members to the group in Azure Active directory using Graph API's in C# but when I run the code I am getting the below error:

ServiceException: Code: BadRequest
Message: Invalid URL format specified in @odata.bind for members

Below is my code:

[AuthorizeForScopes(Scopes = new[] { Constants.GroupMemberReadWriteAll, Constants.DirectoryReadAsUser })]
public async Task<IActionResult> AddMembersToGroup()
{
    MSGraphs.GraphServiceClient graphClient = GetGraphServiceClient(new[] { Constants.GroupMemberReadWriteAll, Constants.DirectoryReadAsUser });

            var group = new MSGraphs.Group
            {
                AdditionalData = new Dictionary<string, object>()
                {
                    {"[email protected]", "[\"https://graph.microsoft.com/v1.0/directoryObjects/c6e5c868-e2bd-46b0-b5f3-c39b5dfe42dc\",\"https://graph.microsoft.com/v1.0/directoryObjects/b2fc878b-a455-4315-b9e1-8594cbad3404\",\"https://graph.microsoft.com/v1.0/directoryObjects/79f06743-3bee-418e-8f4e-b381ebdfafc2\"]"}
                }
            };

            await graphClient.Groups["{29f7e645-8de1-45fc-80a5-e76e0fa51340}"]
                .Request()
                .UpdateAsync(group);

            return Ok();
        }

Let me know if I am missing something here.

Upvotes: 2

Views: 349

Answers (1)

Markus
Markus

Reputation: 22456

Instead of supplying the members as a string (and by that anticipating JSON serialization), you could use a string array:

var group = new MSGraphs.Group
  {
    AdditionalData = new Dictionary<string, object>()
      {
         { 
            "[email protected]", 
            new string[] 
            { 
              "https://graph.microsoft.com/v1.0/directoryObjects/c6e5c868-e2bd-46b0-b5f3-c39b5dfe42dc",
              "https://graph.microsoft.com/v1.0/directoryObjects/b2fc878b-a455-4315-b9e1-8594cbad3404",
              "https://graph.microsoft.com/v1.0/directoryObjects/79f06743-3bee-418e-8f4e-b381ebdfafc2" 
            }
         }
     }
  };

This way, .NET can take care of the serialization afterwards.

Upvotes: 3

Related Questions