3

Been trying to add an AD group to an SPGroup programmatically and it's not working.

I've tried:

SPGroup.AddUsers("myADgroup");

and

SPGroupCollection.Add(groupName, currentUser, "myADgroup", groupDescription);

I've tried it both with domain and without.

Any ideas?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Fongers
  • 155
  • 1
  • 1
  • 9

4 Answers4

5

Erm, there is no SPGroup.AddUsers("myAdGroup") method. There isn't even an AddUser() method with that format.

Have you tried:

SPGroup g = web.AssociatedMemberGroup;
SPUser u = web.EnsureUser("DOMAIN\\myADgroup");
g.AddUser(u);

The EnsureUser bit makes sure that the AD group is added as a user of the SPWeb, so you can then assign rights.

Andy Burns
  • 917
  • 4
  • 8
  • Yes, I've tried EnsureUser and it did not work. Came back with user not found. – Fongers Dec 28 '10 at 23:39
  • Hmm. I'm not an expert with AD, but I'd suggest that if EnsureUser is coming back with user not found, then your group name is wrong/different in AD. I did test that code, and it worked on this machine. Would it be worth checking the name in AD, and that you're specifying the domain as well as the name. Also, notice the escaped slash in the name (" \\ ") – Andy Burns Dec 29 '10 at 15:13
1

Hi there is no methods for now to add AD group.

You need to create group first, aand after that add users to that group.

using (SPSite spSite = new SPSite("http://localhost"))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {

        SPGroupCollection spGrps = spWeb.SiteGroups;
        SPUser uGrpOwner = spWeb.CurrentUser;
        SPUser uGrpDefMember = spWeb.CurrentUser;
        string sGrpName = "GrupeName";
        spGrps.Add(sGrpName, uGrpOwner, uGrpDefMember, "Decription");

        SPGroup spGrp = spGrps[sGrpName];

        List<SPUser> spUsersFromAD = YouFunctionGetUserFromAD(); 

        foreach(SPUser spUser  in  spUsersFromAD){
            spGrp.AddUser(spUser);
        }
        spWeb.Update();
    }
}
Ruslan
  • 154
  • 1
  • 3
1

This is a follow up from Andy Burn's answer:

From PowerShell I initially tried the following:

$web = Get-SPWeb http://localhost
$web.EnsureUser("domain\test group")

This didn't work, which was puzzling.

Some more experimentation and I found the following did work (Power Users is a built in group):

$web.EnsureUser("builtin\power users")
$web.EnsureUser("power users")
$web.EnsureUser("test group")

I then noticed that I had a different value for the Name (aliased as DisplayName in PowerShell) -- it turns out that I had used different values in Active Directory for 'Group name' and 'Group name (pre-Windows 2000)'.

The group name worked by itself, but with the domain prefix I needed to use the pre-Windows 2000 name.

With this, I was able to get the following to work:

$web.EnsureUser("domain\pre2000 test group")

So, if you are still having issues check for consistency between the two group names in AD.

Sly Gryphon
  • 3,751
  • 1
  • 25
  • 17
0

I also experienced a problem with calling SPWeb.EnsureUser with an AD group. In my case there was some confusion because the group in question had a display name that was different to its underlying sAMAccountName. Calling EnsureUser with the sAMAccountName rather than the display name sorted the problem for me.