0

I posted this question on SharePoint exchange but it did not get any attention. Any help will be appreciated.

I have implemented a site and have added a SharePoint group called "SG_Uploader".

In this group, I ONLY have one Active Directory group called "AD_L6" and there are many users in AD_L6.

If a user comes to site and I want to check if he can upload a document, I use below code which is very simple:

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

Now, I want to do the same thing, not for current user but for a specific user that I have his username. By that mean I want to write a code like

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

I could not figure out who I can do that. Please advise.

FLICKER
  • 6,439
  • 4
  • 45
  • 75

2 Answers2

2

The following code for your reference.

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

        }
    }
}
LZ_MSFT
  • 4,079
  • 1
  • 8
  • 9
  • Thanks for reply. This also is not the answer however it is helpful. it only works if username is direct member of adGroupName. if not looks like I have to use a recursive function to find if the username is art of adGroupName. I'm not sure if there is any easier way. I'm afraid that resursivly calling AD functions may have a performance problem in my case. +1 – FLICKER Jul 16 '18 at 21:12
  • If you check if the user is a member of ad group, we have to use some method to connect the AD. – LZ_MSFT Jul 17 '18 at 01:22
  • Do you know what is that method? does it give me the correct result even if the user is not directly part of a group. in my case (in AD), I have group A which has group B and group B has group C and user is member of group C – FLICKER Jul 17 '18 at 16:38
0

You can do that.

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

Visit 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

Yojan Morales
  • 136
  • 2
  • 12