如何以编程方式将 AD 组添加到 SharePoint 2010 SPGroup?

发布于 2024-10-08 23:49:24 字数 266 浏览 2 评论 0原文

尝试以编程方式将 AD 组添加到 SPGroup,但没有成功。

我已经尝试过:

SPGroup.AddUsers("myADgroup");

并且

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

我已经尝试过使用域和不使用域。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫儰鉨ミ守葔 2024-10-15 23:49:24

呃,没有 SPGroup.AddUsers("myAdGroup") 方法。甚至没有 AddUser()具有该格式的方法。

您是否尝试过:

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

EnsureUser 位确保AD 组已添加为 SPWeb 的用户,因此您可以分配权限。

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.

狠疯拽 2024-10-15 23:49:24

您好,目前没有添加 AD 组的方法。

您需要先创建组,然后将用户添加到该组。

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();
    }
}

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();
    }
}
薄情伤 2024-10-15 23:49:24

这是 Andy Burn 的回答的后续内容:

从 PowerShell 我最初尝试了以下操作:

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

这不起作用,这令人费解。

经过更多实验,我发现以下方法确实有效(Power Users 是内置组):

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

然后我注意到名称有不同的值(在 PowerShell 中别名为 DisplayName)——事实证明我使用了不同的值在 Active Directory 中查找“组名称”和“组名称(Windows 2000 之前的版本)”。

该组名称可以单独使用,但对于域前缀,我需要使用 Windows 2000 之前的名称。

有了这个,我能够完成以下工作:

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

因此,如果您仍然遇到问题,请检查 AD 中两个组名称之间的一致性。

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.

回忆躺在深渊里 2024-10-15 23:49:24

我在使用 AD 组调用 SPWeb.EnsureUser 时也遇到了问题。就我而言,存在一些混乱,因为相关组的显示名称与其底层 sAMAccountName 不同。使用 sAMAccountName 而不是显示名称调用 EnsureUser 为我解决了问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文