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:
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.