C# context is already tracking the entity, azure active directory graph api. add group member
Asked Answered
P

1

1

I am trying to add member to azure active directory group but it fails it showing me following error.

context is already tracking the entity

I tried to found lot for it, I also see this links

Azure Active Directory Graph Client 2.0 - Context is not currently tracking the entity

http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx

But I not get any success please help me.

This is my code :

try
        {
            ActiveDirectoryClient client = ADGraphHelper.GetActiveDirectoryClient();
            IGroupFetcher groupFetcher = client.Groups.GetByObjectId(groupId);
            Group group = (Group)(await groupFetcher.ExecuteAsync());
            string[] userIds = userId.Split(',');
            foreach (string id in userIds)
            {
                if (id != "")
                {
                    IUser user = client.Users.Where(u => u.ObjectId == id).ExecuteAsync().Result.CurrentPage.ToArray().First();
                    if (user != null)
                    {
                        //check wather user aleady present into group or not
                        IDirectoryObject userExists = group.Members.Where(u => u.ObjectId == id).FirstOrDefault();
                        if (userExists == null)
                        {
                            group.Members.Add(user as DirectoryObject);
                        }
                    }
                    else
                        throw new Exception("User is null.");
                }
            }
            await group.UpdateAsync();
            return Json(new { success = true });
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", "we connot process your request please contact to support for more details.");
// error handling code.
            return PartialView();
        }
Plaintive answered 23/1, 2016 at 14:13 Comment(0)
P
1

Hey everyone I found what is issue now in this. it was the bug on the old Graph client library. it is solved in the Graph client library 2.0, So please use Graph client library 2.0 or higher.

This is the link where you can download latest graph client library: https://www.nuget.org/packages/Microsoft.Azure.ActiveDirectory.GraphClient/

Plaintive answered 24/1, 2016 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.