Get Permission Levels associated with all Groups in Site – 1

In this post, I will show you have to get the permission level for the Groups associated to the Web Site Level using the Managed Client Object Model.
Managed Client Object Model – Get Permission Levels associated with all Groups in Site

  1. I have my code which supports the Console Application, so create the Console Application in C# Section from Visual Studio.
  2. Then add the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll references. We can obtain those two dll’s from below methods,
    1. From 14 Hive / ISAPI Folder (If we installed SharePoint 2010 in your machine)
    2. If SharePoint 2010, not available on the machine; install “SharePoint Foundation 2010 Client Object Model Redistributable” to get those dll’s.
  3. Open the code page and add the following code,

The following code example displays in a console application, the Permission Levels associated to the Groups for the specified Site.

try
{
string siteURL = "http://SiteUrl";
string username = "username";
string password = "password";
string domain = "domain";

ClientContext ctx = new ClientContext(siteURL);
ctx.Credentials = new System.Net.NetworkCredential(username, password, domain);
Web webSite = ctx.Web;

//Following Load method loads,
//Line 2: Identify the website has Unique Perimission or Inherited Permission
//Line 3: Get the WebSite Title Property
//Line 4: Get the collection of RoleAssignment, associated to the Site
//Get the Member and (collection of RoleDefinition) RoleDefinitionBindingCollection property from RoleAssignment
//Line 5,6: Member Property retrives the Title and Id
//Line 7,8: RoleDefintion retrives the Title Property

//Start: Line 0
ctx.Load(webSite, oweb =>
oweb.HasUniqueRoleAssignments,
oweb => oweb.Title,
oweb => oweb.RoleAssignments.Include(
roleAssignment => roleAssignment.Member.Title,
roleAssignment => roleAssignment.Member.Id,
roleAssignment => roleAssignment.RoleDefinitionBindings.Include(
roleDefinition => roleDefinition.Name)));

//End: Line 9

ctx.ExecuteQuery();

//Code for Output Follows
RoleAssignmentCollection roleAssCollection = webSite.RoleAssignments;
Console.WriteLine("Site Title: " + webSite.Title);
Console.WriteLine("Site has Unique Permissions: " + webSite.HasUniqueRoleAssignments);
Console.WriteLine("Group Name \t Group ID \t Permission Level");
Console.WriteLine("******************************************");
foreach (RoleAssignment roleAss in roleAssCollection)
{
Principal roleMember = roleAss.Member;
Console.Write(roleMember.Title + " \t " + roleMember.Id + " \t ");
foreach (RoleDefinition roleDef in roleAss.RoleDefinitionBindings)
{
Console.Write(roleDef.Name + ";");
}
Console.WriteLine("");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

I have uploaded this Code and Executable file in CodePlex with a project named as SharePoint Permissions

Shantha Kumar
Shantha Kumar
Articles: 280

24,849 Comments

Comments are closed.