Showing posts with label user permission. Show all posts
Showing posts with label user permission. Show all posts

August 08, 2013

SharePoint Get all permission and list of site of SPuser / current user C# programatically

Well I have achieved this by using SPRoles, but as all knows SPRoles has been deprecated from September 2012, giving warning user SPRoleAssignment and SPRoleDefinition class.



Its a bit confusing so lets understand about SPRoleDefinition and SPRoleAssignment.

SPRoleDefinition represents the SharePoint OOB permission levels like "Full Control", "Contribute", "Read"while a Role Assignment contains a collection of Role Definitions ItemAdd, ItemView, UpdateItem, EditPage etc....

Here is a simple code snippet to get current user's / give user's permissions



Code:
string permission = string.Empty;

            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                foreach (SPWeb oWeb in oSite.RootWeb.GetSubwebsForCurrentUser())
                {
                    SPPermissionInfo info = oWeb.GetUserEffectivePermissionInfo(oUser.LoginName);

                    permission = string.Empty;
                    foreach (SPRoleAssignment roleA in info.RoleAssignments)
                    {
                        foreach (SPRoleDefinition roledef in roleA.RoleDefinitionBindings)
                        {
                            permission += roledef.Name.ToString() + ", ";
                        }
                    }
                    permission = " [" + permission.TrimEnd(", ".ToCharArray()) + "]";
           }
     }

It will return users permission from all sites on which user has permission in format of [Read, Contribute]  /  [Read]  /  [Full Control]

oUser is SPUser object that has user details you can pass Currentuser also by using below lines:
SPUser ouser = SPContext.Current.Web.CurrentUser;




Please share any issue or findings on this.



--
Regards,
Praveen Pandit