0

How do I prepare a solution which does this:

When we add a new user to a Sharepoint Group, it should automatically add the user to an Active Directory Group. This should create two groups with the same users

2 Answers 2

2

Why not just have the active directory group as the only member inside of a SharePoint group?

If you want to do it that way, it may have to be an Event Receiver for a Site using the GroupUserAdded event receiver, then you tie into adding that user to Active Directory like so:

using System.DirectoryServices.AccountManagement;
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find your user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "USERNAME");

    if(user != null)
    {
        // find the group in question
        GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "ADGROUPNAME");

        // if found....
        if (group != null)
        {
            // add user to group
            group.Members.Add(user);
            group.Save();
        }
    }
}
1
  • It's nothing do. What the rights to AD should be ?
    – Grzegorz Z
    Commented Aug 26, 2016 at 13:07
0

You need to create a custom user interface for adding users to the SharePoint Group.

You can use SPGroup object for adding SPUser object into the group.

For manipulating Active Directory you can use System.DirectorServices.AccountManagement namespace. This has GroupPrincipal object

using (PrincipalContext p = new PrincipalContext(ContextType.Domain, "domainname"))
{
    GroupPrincipal g= GroupPrincipal.FindByIdentity(p, "GroupName");
    g.Members.Add(p, IdentityType.UserPrincipalName, UserId);
    g.Save();
}
4
  • And You have sample of It. More code. It should be on Sharepoint Page, or in asp.net page ?
    – Grzegorz Z
    Commented Aug 24, 2016 at 13:32
  • 1
    In visual studio create sharepoint project -> Visual Webpart. Now create a webpart page and add the new web part. Commented Aug 24, 2016 at 13:35
  • And use GroupUserAdded Event Receiver ?
    – Grzegorz Z
    Commented Aug 24, 2016 at 13:58
  • It showing me: No principal matching the specified parameters was found.
    – Grzegorz Z
    Commented Aug 26, 2016 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.