2
votes

I have recently installed the Azure Active Directory Graph Client Library 2.0.2 Nuget package and am unable to add Members to Groups both adding a group to a group or adding a user to a group I am getting the following error when the AddLink function is called:

“[System.InvalidOperationException] = {"The context is not currently tracking the entity."}

My code:

IGroup group = azureClient.Groups.GetByObjectId("Guid here").ExecuteAsync().Result;
IGroup groupToAdd = azureClient.Groups.GetByObjectId("Guid here").ExecuteAsync().Result;
azureClient.Context.AddLink(group, "Members", groupToAdd);
azureClient.Context.SaveChanges();

I have been unable to find any mention of this error in relation to the Azure Active Directory Graph Client Library from doing Google searches so any help on this would be much appreciated.

3

3 Answers

5
votes

We’ve just released an update to the Graph client library, that fixes this problem. You should now be able to add members to groups. The mechanism is a little different from using AddLinks (and hopefully simpler).

We also have a new blog describing the client library which talks about this and many other things: http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx

For reference, to add a member to a group:

{groupObject}.Members.Add({entityObject} as DirectoryObject);

So for example, to update a group with a new user member:

myGroup.Members.Add(userToBeAdded as DirectoryObject); await myGroup.UpdateAsync();

NOTE: The same construct can be used to add users to a DirectoryRole object. Groups and users may be added to a group, however, for now, only users can be added to a DirectoryRole.

Hope this helps,

2
votes

I had the same issue, and the documentation wasn't very clear, so maybe this will help others. You cannot add users as members of an IGroup, but only to a Group. You also cannot add IDirectoryObjects to the Members collection, but only DirectoryObjects. You must first cast your IUser and IGroup objects. The following code is what I have working at the moment:

var graphClient = new ActiveDirectoryClient(new Uri(ConfigHelper.GraphServiceRoot), async () => await GetUserTokenAsync(cache));
var actualUser = await graphClient.Users.GetByObjectId(matchedUser.ObjectId).ExecuteAsync();
var actualGroup = (Group) await graphClient.Groups.GetByObjectId(matchedGroup.ObjectId).ExecuteAsync();

actualGroup.Members.Add(actualUser as DirectoryObject);
await graphClient.Context.SaveChangesAsync();
0
votes

I tried this new syntax but still does not work.

 public async Task<Result<dynamic>> addUserToAzureGroup(Group AzGroup, User AzUser)
 {
     // link the found user with the found group
     try
     {
         AzGroup.Members.Add(AzUser as DirectoryObject);
         await AzGroup.UpdateAsync();
     }
     catch (Exception ex)
     {
         Exception myEx = new Exception(ex.Message);
         retResult.Exception = myEx;
         return retResult;
     }
     return retResult;
 }

I have almost the same error text in the execption message: The context is already tracking the relationship

Any news on that issue? Could anyone guess why that happens?

I also tried from the manage.windowsAzure.com UI and still cannot add the user! I get this error: Could not add members to group 'myAzAD_group'.