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.
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.
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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With