Search code examples
c#asp.net-core-mvcmicrosoft-graph-apiodataazure-ad-graph-api

Add users as group members with Microsoft Graph Api in Net Core


In my project I'm using the Microsoft Graph Api in my Net Core 3.0 project to connect to Azure Intune and add groups and users. When adding a group with members, the graph api requires a json representation of users, like this ( documentation):

var group = new Group
{
    // other properties omitted
    AdditionalData = new Dictionary<string, object>()
    {
        {"owners@odata.bind", "[\"https://graph.microsoft.com/v1.0/users/26be1845-4119-4801-a799-aea79d09f1a2\"]"},
        {"members@odata.bind", "[\"https://graph.microsoft.com/v1.0/users/ff7cb387-6688-423c-8188-3da9532a73cc\",\"https://graph.microsoft.com/v1.0/users/69456242-0067-49d3-ba96-9de6f2728e14\"]"}
    }
};

Brief edit: I tried the code above from the docs, replacing the Guids with the users I want to add, and this didn't work either, giving me the same error message. End edit.

How do I add the members dynamically in the dictionary, say from an array of user-id's? They seem to use escape characters, and using JsonConvert.SerializeObject(arrayObjectWithIds) doesn't seem to work, since I get an inproperly formatted OData field back from the Graph Api: Invalid URL format specified in @odata.bind for members

What i have:

string[] memberIds = new string[] { "https://graph.microsoft.com/v1.0/users/123", "https://graph.microsoft.com/v1.0/users/456", "https://graph.microsoft.com/v1.0/users/789" };
string json = JsonConvert.SerializeObject(memberIds);

newGroup.AdditionalData = new Dictionary<string, object>()
{
    {"members@odata.bind", json }
};

// Send it off and get Invalid URL format specified in @odata.bind for members error

This is my json as it is currently attached to the dictionary:

["https://graph.microsoft.com/v1.0/users/123\"","https://graph.microsoft.com/v1.0/users/456\"","https://graph.microsoft.com/v1.0/users/789\""]

What is a proper way to put the member uri's into the dictionary object?


Solution

  • The problem was caused by the escape character \" in the code, I test the same code from the document and also see the same error message Invalid URL format specified in @odata.bind for members. So I modified my code as below:

    var additionalData = new Dictionary<string, object>()
        {
            {"owners@odata.bind", new List<string>()},
            {"members@odata.bind", new List<string>()}
        };
    (additionalData["members@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/xxxxx");
    (additionalData["owners@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/xxxxx");
    
    var group = new Group
    {
        Description = "Group with designated owner and members",
        DisplayName = "huryNewGroup",
        GroupTypes = new List<String>()
        {
            "Unified"
        },
        MailEnabled = true,
        MailNickname = "operations2019",
        SecurityEnabled = false,
        AdditionalData = additionalData
    };
    

    Running the code, I created the group with members successfully.

    By the way, we may run into the permission problem. At first I only added the permission Group.ReadWrite.All for the app but it shows I don't have permission when I run the code. Then I added the other permissions Directory.ReadWrite.All, Directory.AccessAsUser.All, it works fine.(As far as I know, there are some minor problems with the Group permissions, so you'd better add the other Directory permissions)

    Hope it helps~