The following example returns all the Groups with permissions associated to the SharePoint Web using Managed Client Side Object Model.
After creating a console application project in Visual Studio solution, add the following assembly references to the project.
Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime
- using Microsoft.SharePoint.Client;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
-
- namespace GetWebGroups
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Console.Write("Enter Site URL: ");
- string strURL = Console.ReadLine();
-
-
- Console.Write("Enter UserName (domain/userid): ");
- string strUserName = Console.ReadLine();
-
- Console.Write("Enter your password: ");
- string pass = getPassword();
- Console.WriteLine();
-
- ClientContext ctx = new ClientContext(strURL);
- ctx.Credentials = new NetworkCredential(strUserName, pass);
- Web web = ctx.Web;
-
-
- ctx.Load(web, w => w.Title);
- RoleAssignmentCollection roleAssignments= web.RoleAssignments;
-
-
- ctx.Load(roleAssignments, roleAssignement => roleAssignement.Include(r => r.Member, r => r.RoleDefinitionBindings));
- ctx.ExecuteQuery();
-
- Console.WriteLine("Groups has permission to the Web: " + web.Title);
- Console.WriteLine("Groups Count: " + roleAssignments.Count.ToString());
- Console.WriteLine("Group with Permissions as follows:");
- foreach (RoleAssignment grp in roleAssignments)
- {
- string strGroup = "";
- strGroup += grp.Member.Title +" : ";
-
- foreach (RoleDefinition rd in grp.RoleDefinitionBindings)
- {
- strGroup += rd.Name+ " ";
- }
- Console.WriteLine(strGroup);
- }
- Console.Read();
- }
-
- private static string getPassword()
- {
- ConsoleKeyInfo key;
- string pass = "";
- do
- {
- key = Console.ReadKey(true);
-
- if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
- {
- pass += key.KeyChar;
- Console.Write("*");
- }
- else
- {
- if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
- {
- pass = pass.Substring(0, (pass.Length - 1));
- Console.Write("\b \b");
- }
- }
- }
-
- while (key.Key != ConsoleKey.Enter);
- return pass;
- }
- }
- }