Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security;
  5. using Microsoft.SharePoint.Client;
  6. using Microsoft.SharePoint.Client.Utilities;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. ConsoleColor defaultForeground = Console.ForegroundColor;
  15.  
  16. Console.ForegroundColor = ConsoleColor.Green;
  17. Console.WriteLine("Enter the URL of the SharePoint Online site:");
  18.  
  19. Console.ForegroundColor = defaultForeground;
  20. var webUrl = Console.ReadLine();
  21. if (string.IsNullOrWhiteSpace(webUrl)) webUrl = null;
  22.  
  23. Console.ForegroundColor = ConsoleColor.Green;
  24. Console.WriteLine("Enter your user name (ex: user@tenant):");
  25. Console.ForegroundColor = defaultForeground;
  26. var userName = Console.ReadLine();
  27. if (string.IsNullOrWhiteSpace(userName)) userName = null;
  28.  
  29. Console.ForegroundColor = ConsoleColor.Green;
  30. Console.WriteLine("Enter your password.");
  31. Console.ForegroundColor = defaultForeground;
  32. var password = GetPasswordFromConsoleInput();
  33.  
  34. using (var context = new ClientContext(webUrl ?? "default_url"))
  35. {
  36. context.Credentials = new SharePointOnlineCredentials(userName ?? "default_username", password);
  37. var web = context.Web;
  38.  
  39. var permissions = new BasePermissions();
  40. permissions.Set(PermissionKind.EditListItems);
  41.  
  42. var result = new List<string>();
  43. result.Add("User|Url");
  44. ScanWeb(context, web, ref result, true);
  45. result.ForEach(Console.WriteLine);
  46.  
  47. System.IO.File.WriteAllLines(string.Format(@"C:\\Contributors_{0}.txt",
  48. DateTime.Now.ToString("yyyy-MM-dd-ssfff")), result.ToArray());
  49.  
  50. Console.WriteLine("Done!");
  51. Console.Read();
  52. }
  53. }
  54.  
  55. private const string LineFormat = @"{0}|{1}";
  56.  
  57. private static void ScanWeb(ClientContext ctx, Web web, ref List<string> result, bool rootweb)
  58. {
  59. ctx.Load(web, w => w.HasUniqueRoleAssignments, w => w.Url);
  60. ctx.Load(web.Webs);
  61. ctx.Load(web.RoleAssignments);
  62. ctx.Load(web.SiteGroups);
  63. ctx.ExecuteQuery();
  64.  
  65. if (rootweb || web.HasUniqueRoleAssignments)
  66. {
  67. var roles = web.RoleAssignments;
  68. foreach (var role in roles)
  69. {
  70. ctx.Load(role.Member);
  71. ctx.Load(role.RoleDefinitionBindings);
  72. ctx.ExecuteQuery();
  73. ScanRole(ctx, web, role, ref result);
  74. }
  75. }
  76.  
  77. foreach (var w in web.Webs)
  78. ScanWeb(ctx, w, ref result, false);
  79. }
  80.  
  81. private static void ScanRole(ClientContext ctx, Web web, RoleAssignment role, ref List<string> result)
  82. {
  83. var groups = web.SiteGroups;
  84.  
  85. if (!role.RoleDefinitionBindings.Any(r => r.BasePermissions.Has(PermissionKind.EditListItems)))
  86. return;
  87.  
  88. if (role.Member.PrincipalType == PrincipalType.User)
  89. result.Add(string.Format(LineFormat, role.Member.Title, web.Url));
  90. else if (role.Member.PrincipalType == PrincipalType.SharePointGroup)
  91. {
  92. var g = groups.GetByName(role.Member.LoginName);
  93. ctx.Load(g);
  94. ctx.Load(g.Users);
  95. ctx.ExecuteQuery();
  96.  
  97. foreach (var user in g.Users)
  98. result.Add(string.Format(LineFormat, user.Title, web.Url));
  99. }
  100. }
  101.  
  102. private static SecureString GetPasswordFromConsoleInput()
  103. {
  104. ConsoleKeyInfo info;
  105.  
  106. //Get the user's password as a SecureString
  107. SecureString securePassword = new SecureString();
  108. do
  109. {
  110. info = Console.ReadKey(true);
  111. if (info.Key != ConsoleKey.Enter)
  112. {
  113. securePassword.AppendChar(info.KeyChar);
  114. }
  115. }
  116. while (info.Key != ConsoleKey.Enter);
  117. return securePassword;
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement