Check Current User Permissions…


I had a requirement to security trim some functionality in SharePoint to some user roles. Basically I wanted to display some controls / options in the Ribbon control to the users if they have “Design” permission level.

One thing to understand here is, SharePoint Security Groups has a group named, Designer. Checking is the current user in the Designer group will not be accurate. Because there can be new groups get created with the same permission set but with a different name. Also SharePoint allows you to add direct permission to users. (Not adding to a Group but directly give required permissions to the user itself).

So as a solution we have to check the current users Permission Levels contains “Design” permission level.

Following is a code segment that can be used to full fill the above requirement.

// Retrieve all user roles assigned for the current user for the current web. 
SPRoleDefinitionBindingCollection userRoles = 
                                  SPContext.Current.Web.AllRolesForCurrentUser;

// Retrieve all role definitions in the current web. 
SPRoleDefinitionCollection roleDefinitions = 
                                  SPContext.Current.Web.RoleDefinitions;
SPRoleDefinition roleDefinition = roleDefinitions[PERMISSION_LEVEL_DESIGN];

// Check current user permissions and the required permissions. 
if (!userRoles.Contains(roleDefinition))
{
    // Hide the controls from ribbon for the non designer users.
}

Simple Smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s