Share via

How IsCurrentUserMemberOfGroup(int group ID) sharepoint 2010 in SSOM works?

Praveen 20 Reputation points
2023-09-27T13:22:28.1366667+00:00

How IsCurrentUserMemberOfGroup(int group ID) sharepoint 2010 in SSOM works and How can I implement same scenario if i want to check user permission using people picker field whether user is in group or not as user is added in ADgroup and that ADgroup is added in SharePoint group.

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | SharePoint Server | Development

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,686 Reputation points Microsoft External Staff
    2023-09-28T06:57:30.47+00:00

    Hi @Praveen ,

    The IsCurrentUserMemberOfGroup() method in SharePoint 2010's Server-Side Object Model (SSOM) checks if the current user is a member of a specific SharePoint group. It returns a boolean value indicating whether the user is a member of the group or not. To use this method, you need to provide the group name or the group ID as a parameter. Here's an example of how to use it in PowerShell:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
    
    # Get the current SharePoint web 
    $web = Get-SPWeb "http://yoursharepointsite.com" 
    
    # Get the SharePoint group by name 
    $groupName = "Your Group Name" 
    $group = $web.SiteGroups[$groupName] 
    
    # Check if the current user is a member of the group 
    $isMember = $group.ContainsCurrentUser 
    
    # Output the result 
    Write-Host "Current user is a member of the group: $isMember" 
    
    # Dispose the SharePoint web object 
    $web.Dispose()
    

    To check if a user is a member of a SharePoint group using a people picker field, you can retrieve the user's AD group membership using PowerShell and then check if that AD group is a member of the SharePoint group. Here's an example PowerShell code to check if a user is a member of a SharePoint group using a people picker field:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    # Get the current SharePoint web
    $web = Get-SPWeb "http://yoursharepointsite.com"
    
    # Get the SharePoint group by name
    $groupName = "Your Group Name"
    $group = $web.SiteGroups[$groupName]
    
    # Get the user from the people picker field
    $userField = $web.Fields.GetFieldByInternalName("Your People Picker Field Name")
    $userFieldValue = $userField.GetFieldValueAsText($web.Lists["Your List Name"].ItemById("Your Item ID"))
    
    # Get the AD group membership of the user
    $adUser = New-Object System.Security.Principal.NTAccount($userFieldValue)
    $adGroup = "Your AD Group Name"
    $isAdGroupMember = $adUser.Translate([System.Security.Principal.SecurityIdentifier]).IsMemberOf((New-Object System.Security.Principal.NTAccount($adGroup)).Translate([System.Security.Principal.SecurityIdentifier]))
    
    # Check if the AD group is a member of the SharePoint group
    $isMember = $group.ContainsGroup($adGroup)
    
    # Output the result
    Write-Host "User is a member of the SharePoint group: $isMember"
    
    # Dispose the SharePoint web object
    $web.Dispose()
    

    For your reference:

    https://learn.microsoft.com/en-us/sharepoint/dev/general-development/authorization-users-groups-and-the-object-model-in-sharepoint

    Hope this helps.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.