Monday, October 25, 2010

Programmatically get permissions for all the users using client object model

Programmatically get permissions for all the users using client object model.

Firstly, to better understand about SPRoleDefinition and SPRoleAssignment read below.

SPRoleDefinition represents the levels such as “Full Control”, “Contribute”, while a Role
Assignment contains a collection of Role Definitions.
Any object that inherits from SPPrincipal can be assigned a SPRoleAssignment. For e.g. an
SPPrincipal such as a logged in user(SPUser type) called “isha”, can have a role assignment that
points you to two SPRoleDefinitions, “Full Control” and “Design”, thereby giving you a union
of SPBasePermissions between Full Control and Design.

Here is a simple code snippet to get SPRoleDefinitions for all the users in a site.

private static void GetUserRoles()
{
using (SPSite site = new SPSite(SPsiteUrl))
{

SPWeb web = site.OpenWeb();
Console.WriteLine(“\n\n Roles Assignments:”);

foreach (SPRoleAssignment roleA in web.RoleAssignments)
{
Console.WriteLine(“The following Role definition bindings exist for ” +
roleA.Member.Name);
foreach (SPRoleDefinition roledef in roleA.RoleDefinitionBindings)
{
Console.WriteLine(roledef.Name);
}
}
Console.ReadLine();
}
}

2 comments: