如何列出 SPGroup 中的所有 SPUser 对象?

发布于 2024-09-13 16:23:28 字数 377 浏览 4 评论 0原文

我需要从 SPGroup 检索所有 SPUser。不幸的是,该组可能包含 Active Directory 组,因此简单的 SPGroup.Users 是不够的(我只需为 AD 组获取一个 SPUser ,其中 < code>IsDomainGroup 属性设置为 true)。

有谁知道如何获取所有 SPUser 的列表,并深入到 SPGroup 中包含的任何 Active Directory 组?是否有替代采用 SPUser 参数的 SPGroup.ContainsCurrentUser 的方法?

I need to retrieve all SPUser's from a SPGroup. Unfortunately, the group may contain Active Directory groups, so a simple SPGroup.Users is not enough (I'd just get a single SPUser for the AD group, with the IsDomainGroup property set to true).

Does anyone have a good idea how can I obtain a list of all SPUser's, descending into any Active Directory groups contained in a SPGroup? Is there an alternative to SPGroup.ContainsCurrentUser that takes a SPUser parameter?

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

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

发布评论

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

评论(2

鹿! 2024-09-20 16:23:28

基于我发现的博客文章,我编写了以下代码:

private static List<SPUser> ListUsers(SPWeb web, SPPrincipal group)
{
    try
    {
        web.Site.CatchAccessDeniedException = false;
        var users = new List<SPUser>();
        foreach(SPUser user in web.SiteUsers)
        {
            using(var userContextSite = new SPSite(web.Site.ID, user.UserToken))
            {
                try
                {
                    using (var userContextWeb = userContextSite.OpenWeb(web.ID))
                    {
                        try
                        {
                            if (userContextWeb.SiteGroups[group.Name]
                                .ContainsCurrentUser)
                                    users.Add(user);
                        }
                        catch (SPException)
                        {
                            // group not found, continue
                        }
                    }
                }
                catch(UnauthorizedAccessException)
                {
                    // user does not have right to open this web, continue
                }
            }
        }
        return users;
    }
    finally
    {
        web.Site.CatchAccessDeniedException = true;
    }
}

我不喜欢必须模拟每个用户的事实,并且此代码只会查找已导入 SharePoint 的 AD 用户(因此 SPUser 对他们来说存在),但这对我来说已经足够了。

Based on a blog post I found, I have written the following code:

private static List<SPUser> ListUsers(SPWeb web, SPPrincipal group)
{
    try
    {
        web.Site.CatchAccessDeniedException = false;
        var users = new List<SPUser>();
        foreach(SPUser user in web.SiteUsers)
        {
            using(var userContextSite = new SPSite(web.Site.ID, user.UserToken))
            {
                try
                {
                    using (var userContextWeb = userContextSite.OpenWeb(web.ID))
                    {
                        try
                        {
                            if (userContextWeb.SiteGroups[group.Name]
                                .ContainsCurrentUser)
                                    users.Add(user);
                        }
                        catch (SPException)
                        {
                            // group not found, continue
                        }
                    }
                }
                catch(UnauthorizedAccessException)
                {
                    // user does not have right to open this web, continue
                }
            }
        }
        return users;
    }
    finally
    {
        web.Site.CatchAccessDeniedException = true;
    }
}

I don't like the fact that I have to impersonate every single user, and this code will only find AD users that have already been imported into SharePoint (so an SPUser exists for them), but that's good enough for me.

如痴如狂 2024-09-20 16:23:28

不幸的是,可能并非 AD 组的每个成员在站点中都有相应的 SPUser 对象。

在这种情况下,我将枚举 Active Directory 组的所有成员,并使用 SPWeb 的 EnsureUser() 方法强制他们进入站点,该方法返回 SPUser,如果站点中尚不存在,则创建一个新的。

有关枚举 Active Directory 成员的指南,请参阅 从给定 AD 组中的 Active Directory 获取用户列表

Unfortunately, it may be the case that not every member of the AD group has a corresponding SPUser object in the site (yet).

In this scenario, I'd enumerate all the members of the active directory group, and force them into the site with the SPWeb's EnsureUser() method, which returns an SPUser, and creates a new one if it doesn't already exist in the site.

For guidance on enumerating active directory members, see Get List of Users From Active Directory In A Given AD Group.

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