Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint: find out if current user belongs to specified group

Tags:

sharepoint

How can I find out programmatically if current user belongs to some group on sharepoint website?
I need it because I would like to show a different content for the users belonging to one group.

like image 848
agnieszka Avatar asked Sep 04 '09 16:09

agnieszka


People also ask

How do I find the group ID in SharePoint?

By default mailNickname is used for the site URL. So if the site URL is https://tenant.sharepoint.com/sites/MyTeamSite then GET https://graph.microsoft.com/v1.0/groups?$filter=mailNickname eq 'MyTeamSite'&$select=id,mailNickname Graph API endpoint will give you the Group ID and Group name.

What is default members group in SharePoint?

A team site by default has three SharePoint groups: Owners, Members and Visitors. These groups have different permissions on the site. By default, SharePoint users are Members and have Edit permission.


2 Answers

I stumbled upon your post because I have (IMHO) the exact same question, but the replies seem somehow not to match that. So I went on searching and found http://www.eggheadcafe.com/conversation.aspx?messageid=30460140&threadid=30420861:

SPWeb site = SPContext.Current.Web;
SPGroup managerGroup = site.Groups["SP_Project_Manager"];
bool isManager = site.IsCurrentUserMemberOfGroup(managerGroup);

As of 2013, according to s654m's comment, the signature seems to have changed:

bool isManager = site.IsCurrentUserMemberOfGroup(managerGroup.ID);
like image 105
chiccodoro Avatar answered Oct 07 '22 09:10

chiccodoro


maybe this code sample post in the ASP.NET Forums helps.

A method you could use

/// <summary>
/// This private method get users by selected SPGroup object.
/// </summary>
/// <param name="group">SPGroup object</param>

private void UsersList(SPGroup group)
{
           foreach(SPUser singleUser in group.Users)
           {
                       foreach(SPRole singleRole in singleUser.Roles)
                       {
                                   _usersListCollection.Add(new UserListCollection(
                                   singleUser.LoginName,singleRole.Name,group.ParentWeb.Title));
                       }
           }
}

Good luck,
Henrik

like image 21
Henrik P. Hessel Avatar answered Oct 07 '22 11:10

Henrik P. Hessel