0

我在 SharePoint Exchange 上发布了这个问题,但没有引起任何注意。任何帮助将不胜感激。

我已经实现了一个站点并添加了一个名为“ SG_Uploader”的 SharePoint 组。

在这个组中,我ONLY有一个名为“”的 Active Directory 组AD_L6,并且 AD_L6 中有很多用户。

如果用户来到网站,我想检查他是否可以上传文档,我使用下面的代码,非常简单:

SPWeb web = // ...
SPGroup group = web.SiteGroups["SG_Uploader"];
if (group.ContainsCurrentUser)
{
    // allow user to upload
}

现在,我想做同样的事情,不是针对当前用户,而是针对我拥有他的用户名的特定用户。这意味着我想写一个像

SPWeb web = // ...
SPGroup group = web.SiteGroups["SG_Uploader"];
if (group.ContainsUser(username))
{
    // allow user to upload
}

我想不出谁能做到这一点。请指教。

4

2 回答 2

2

以下代码供您参考。

var username = "user1";
var spGroupName = "SG_Uploader";
var adGroupName = "AD_L6";
using (SPSite spSite = new SPSite("http://sp2013/sites/team/"))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {
        SPUser user = spWeb.EnsureUser(adGroupName);
        if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(spGroupName)))
        {

            var principalContext = new PrincipalContext(ContextType.Domain);
            var group = GroupPrincipal.FindByIdentity(principalContext, adGroupName);
            var  isGroupMember = group.Members.Any(x => x.Name == username);
            if (isGroupMember)
            {
                Console.WriteLine("User " + username + " is a member of group " + spGroupName);
            }
            else 
            {
                Console.WriteLine("User " + username + " is not a member of group "+spGroupName);
            }

        }
    }
}
于 2018-07-16T05:31:54.227 回答
0

你可以这样做。

  string userName = "PERSEUS\\dmitry.kaloshin";
        string groupName = "Home Members";
        using (SPSite spSite = new SPSite("http://perseus"))
        {
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPUser user = spWeb.EnsureUser(userName);
                if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName)))
                {
                    Console.WriteLine("User " + userName + " is a member of group " + groupName);
                }
                else
                {
                    Console.WriteLine("User " + userName + " is NOT a member of group " + groupName);
                }
            }
        }

访问 https://social.msdn.microsoft.com/Forums/office/en-US/65066c08-9924-4935-9bba-f715b75d3fac/how-to-check-if-user-exists-in-a-particular-sharepoint -group-or-not-programatically?forum=sharepointdevelopmentprevious

于 2018-07-14T03:24:02.643 回答