Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to find permission level for specific user in site collection and subsite using visual studio?

share|improve this question
add comment

3 Answers

Getting Permission levels for a site

Adding or Removing Permissions for a user or group

Custom permission levels

If you go through above links, I am pretty sure you will figure out code you need for what you're trying to do.

Hope it helps.

share|improve this answer
add comment
up vote 1 down vote accepted

Found Solution..

public static string FindPermissionLevelForUser(string loginname, string strSite)

    {
        string strValue = string.Empty;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(strSite))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string spreturnUser = string.Empty;

                        try
                        {
                            SPUser user = web.Users[loginname];
                            SPRoleAssignment currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(user);


                            foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
                            {
                                strValue += role.Name + ", ";
                            }
                        }
                        catch (Exception)
                        {
                            //if user in group
                            SPGroupCollection groupColl = web.Groups;

                            foreach (SPGroup group in groupColl)
                            {
                                try
                                {
                                    SPUser user = group.Users[loginname];
                                    SPRoleAssignment currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(group);

                                    foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
                                    {
                                        strValue += role.Name + ", ";
                                    }
                                }
                                catch (Exception)
                                {

                                }
                            }

                        }
                    }
                }
            });
        }
        catch (Exception ex)
        {

            return string.Empty;

        }
        return strValue;
    }
share|improve this answer
add comment

You can get the permissions for current user using this code

SPBasePermissions bp = 
SPContext.Current.Web.GetUserEffectivePermissions
    (SPContext.Current.Web.CurrentUser.LoginName);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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