2

How can I do a check that looks if current user is a member group or above member group on current site?

Above member group I mean site owner group etc..

So far I have done:

var currentSite = SPContext.Current.Site;
var currentUser = SPContext.Current.Web.CurrentUser.LoginName;

Any kind of help is appreciated

1
  • I can give you a javascript which checks the groupID the current user is in. Or has it to be c#? Aug 1, 2013 at 9:58

2 Answers 2

2

You can use the SPWeb.DoesUserHavePermissions method together with the SPBasePermissions enumeration.

Eg:

bool isUserOwner = SPContext.Current.Web.DoesUserHavePermissions(SPBasePermissions.ManageWeb);

This method will return true if the current user has the permission specified in the parameter. Have a look at the permissions enumeration to determine which is the best permission for your scenario.

1

The following code demonstrates how to verify if the current user is in a particular role or has been assigned a specific permission level.

The code sample uses SPContext.Current to get a reference to the current site, as such it must be rUn under the context of SharePoint (in a web part, or custom application page), to run the code in a console application or windows application you will need to change how the reference to the SPWeb object is obtained

SPWeb web = SPContext.Current.Web; 

//**************************************** 
// Get a reference the roles that are 
// bound to the current user and the role 
// definition to which we need to verify 
// the user against 
SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser; 
SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions; 
SPRoleDefinition roleDefinition = roleDefinitions["Full Control"]; 

// Check if the user is in the role. If not 
// redirect the user to the access denied page 
if (usersRoles.Contains(roleDefinition)) 
{ 
   //******************************* 
   //Check if post back to run 
   //code that initiates the page 
   if (IsPostBack != true) 
   { 
    //Do your stuff here 
   } 
} 
else 
{ 
    Response.Redirect(“/_layouts/accessdenied.aspx”); 
}
1
  • Is it possible to do it in way that it checks Contribute and above permissions? I dont like it to be hardcoded values
    – Rohander
    Aug 1, 2013 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.