Tuesday, August 27, 2013

How to get current logged in user’s group name in SharePoint

Based on my project requirement i need the current logged in user's group name so that we can hide and show something on page load.

Method 1

Dim _site As New SPSite(SPContext.Current.Site.Url)
Dim web As SPWeb = _site.OpenWeb()
For Each gr As SPGroup In web.Groups
If SPContext.Current.Web.IsCurrentUserMemberOfGroup(gr.ID) Then
If (gr.Name.ToString.Trim.ToLower = "Owners".ToString.Trim.ToLower) Then
actionbutton.Visible = True ' hide here 
End If
End If
Next


The above method will get all group's from Current site ..then checking  user is member of any group  if it match  checking for particular group name   to hide somthing in page


Method 2

For Each group As SPGroup In SPContext.Current.Web.CurrentUser.Groups
Dim groupName As String = group.Name
Response.Write(groupName)
Next

 The above method will get all group's which belongs to Current Login User Directly

No comments:

Post a Comment