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.

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,340 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,685 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,971 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,608 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,941 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 26,016 Reputation points Microsoft Vendor
    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.


Your answer

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