Wednesday, May 6, 2009

How to get Roles and permission for SharePoint site/web

There are two powerful classes into SharePoint Object model, one is SPRoleAssignmentCollection and second one is SPRoleAssignment, we can use this classes and find permission of any SharePoint objects, starting from Site, Web, Document Libraraies, etc..

Here I am writing sample code to find all the users permission into current web.

Sample Code:

  using (SPSite site = new SPSite("http://sitename/"))

            {

                using (SPWeb objMainWeb = site.OpenWeb())

                {

 

                       SPRoleAssignmentCollection oRoleAssignments = objMainWeb.RoleAssignments;

             foreach (SPRoleAssignment oRoleAssignment in oRoleAssignments)

            {

                             // SPRoleType provide enumaration for finding all user roles like  Administrators, Designer, Contributors, Visitors

                        //check oRoleAssignment.RoleDefinitionBindings.Contains(objMainWeb.RoleDefinitions.GetByType(SPRoleType.Administrator)) value
                   //  if current role is FULL CONTROL/Administrators then it will return TRUE

                SPPrincipal oPrincipal = oRoleAssignment.Member;

                  // oPrincipal object which will return SPuser or SPGroup, depending on current object

                             }

                     }

           }

        

Reference Link:

Finding roles and permission for list
http://dotnet.org.za/zlatan/archive/2007/12/23/getting-roles-and-permissions-on-a-list-document-library-level-in-sharepoint-2007-or-wss-3-0.aspx

 

Get RoleCollection for Web Site
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.roleassignments.aspx

 

What are the changes in authorization model into WSS 3.0(introduction of new class hierarchy)
http://msdn.microsoft.com/en-us/library/ms469194.aspx

 

How to get SPUser, SPGroup, SPRole objects
http://forums.asp.net/p/1012092/3068708.aspx

4 comments:

  1. [...] How to get Roles and permission for SharePoint site/web. Fuente: Blog de Sanket Shah. [...]

    ReplyDelete
  2. [...] How to get Roles and permission for SharePoint site/web. Fuente: Blog de Sanket Shah. [...]

    ReplyDelete
  3. piece of sharepointJune 17, 2009 at 9:00 AM

    what does this have to do with spquery?

    ReplyDelete
  4. what do you mean by this? can you please explain? thanks!

    ReplyDelete