Since there is no method to test whether a user is a member of a specific SharePoint Site Group such as Reader, Member, etc., use the following method:
private bool IsCurrentUserInRole(string role)
{
bool inRole= false;
SPWeb rootWeb = SPControl.GetContextSite(Context).RootWeb;
SPRole spRole = rootWeb.Roles[role];
SPUser currentUser = rootWeb.CurrentUser;
foreach(SPUser roleUser in spRole.Users)
{
if(roleUser.ID.Equals(currentUser.ID))
{
inRole= true;
break;
}
}
return inRole;
}
A similar loop will have to be used to remove a user from a role. This avoids having to trap an exception that gets thrown when the user cannot be found in the role collection.
Update: I found this post. Take a look.
--Mark
Posted
Wed, Feb 16 2005 12:04 PM
by
Mark DiGiovanni