In your day today development you came across situation like
you have a group name and you want to know the Permission levels(Role
Definition) of that group.
In a following example we are going to have a demo on get
the permission levels base on group name.
From the Main() Method:
List<string> role = new List<string>();
Program obj = new Program();
role = obj.GetRoleDefinition("MyProject Members");
if (role != null)
{
if (role.Count > 0)
{
Console.WriteLine("Role Defination:" +
role[0]);
}
}
Method:
/// <summary>
/// Used to get the Permission level of a group
/// </summary>
/// <param
name="groupName">name of the Group</param>
/// <returns>Permission Levels</returns>
public List<string> GetRoleDefinition(string groupName)
{
try
{
using (SPSite oSite = new SPSite("SiteCollectionURL"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
List<string> roleDef = new List<string>();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPRoleAssignment
roleAssignment in from SPRoleAssignment roleAssignment in oWeb.RoleAssignments where roleAssignment.Member is SPGroup where roleAssignment.Member.Name.Equals(groupName) select roleAssignment)
{
roleDef.AddRange(from SPRoleDefinition roleDefinition in roleAssignment.RoleDefinitionBindings select
roleDefinition.Name);
}
});
return roleDef;
}
}
}
catch (Exception ex)
{
throw;
}
}
Final Output:
Following simple console application will let us know about
the requirement. In a attached Console after downloading and opening in VS.
Please change the Site Collection Url and Group name and just run.You will see
the out put.
Nice Article
ReplyDeleteThanks @Rambabu
Delete