Reputation: 693
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>()
{
{"[email protected]", "[\"https://graph.microsoft.com/v1.0/users/26be1845-4119-4801-a799-aea79d09f1a2\"]"},
{"[email protected]", "[\"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>()
{
{"[email protected]", 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?
Upvotes: 3
Views: 4015
Reputation: 11
For anyone that has the same issue with the Java example, switch from JsonPrimitive to a JsonArray.
So instead of using Microsoft's example code:
group.additionalDataManager().put("[email protected]",
new JsonPrimitive("[\"https://graph.microsoft.com/v1.0/directoryObjects/{id}\",
\"https://graph.microsoft.com/v1.0/directoryObjects/{id}\",
\"https://graph.microsoft.com/v1.0/directoryObjects/{id}\"]"));
Use something like this:
JsonArray arrayOfUsers = new JsonArray();
arrayOfUsers.add("https://graph.microsoft.com/v1.0/directoryObjects/{id1}";
arrayOfUsers.add("https://graph.microsoft.com/v1.0/directoryObjects/{id2}";
group.additionalDataManager().put("[email protected]", arrayOfUsers );
Upvotes: 0
Reputation: 15754
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>()
{
{"[email protected]", new List<string>()},
{"[email protected]", new List<string>()}
};
(additionalData["[email protected]"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/xxxxx");
(additionalData["[email protected]"] 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~
Upvotes: 11