Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security;
- using Microsoft.SharePoint.Client;
- using Microsoft.SharePoint.Client.Utilities;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- ConsoleColor defaultForeground = Console.ForegroundColor;
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Enter the URL of the SharePoint Online site:");
- Console.ForegroundColor = defaultForeground;
- var webUrl = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(webUrl)) webUrl = null;
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Enter your user name (ex: user@tenant):");
- Console.ForegroundColor = defaultForeground;
- var userName = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(userName)) userName = null;
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Enter your password.");
- Console.ForegroundColor = defaultForeground;
- var password = GetPasswordFromConsoleInput();
- using (var context = new ClientContext(webUrl ?? "default_url"))
- {
- context.Credentials = new SharePointOnlineCredentials(userName ?? "default_username", password);
- var web = context.Web;
- var permissions = new BasePermissions();
- permissions.Set(PermissionKind.EditListItems);
- var result = new List<string>();
- result.Add("User|Url");
- ScanWeb(context, web, ref result, true);
- result.ForEach(Console.WriteLine);
- System.IO.File.WriteAllLines(string.Format(@"C:\\Contributors_{0}.txt",
- DateTime.Now.ToString("yyyy-MM-dd-ssfff")), result.ToArray());
- Console.WriteLine("Done!");
- Console.Read();
- }
- }
- private const string LineFormat = @"{0}|{1}";
- private static void ScanWeb(ClientContext ctx, Web web, ref List<string> result, bool rootweb)
- {
- ctx.Load(web, w => w.HasUniqueRoleAssignments, w => w.Url);
- ctx.Load(web.Webs);
- ctx.Load(web.RoleAssignments);
- ctx.Load(web.SiteGroups);
- ctx.ExecuteQuery();
- if (rootweb || web.HasUniqueRoleAssignments)
- {
- var roles = web.RoleAssignments;
- foreach (var role in roles)
- {
- ctx.Load(role.Member);
- ctx.Load(role.RoleDefinitionBindings);
- ctx.ExecuteQuery();
- ScanRole(ctx, web, role, ref result);
- }
- }
- foreach (var w in web.Webs)
- ScanWeb(ctx, w, ref result, false);
- }
- private static void ScanRole(ClientContext ctx, Web web, RoleAssignment role, ref List<string> result)
- {
- var groups = web.SiteGroups;
- if (!role.RoleDefinitionBindings.Any(r => r.BasePermissions.Has(PermissionKind.EditListItems)))
- return;
- if (role.Member.PrincipalType == PrincipalType.User)
- result.Add(string.Format(LineFormat, role.Member.Title, web.Url));
- else if (role.Member.PrincipalType == PrincipalType.SharePointGroup)
- {
- var g = groups.GetByName(role.Member.LoginName);
- ctx.Load(g);
- ctx.Load(g.Users);
- ctx.ExecuteQuery();
- foreach (var user in g.Users)
- result.Add(string.Format(LineFormat, user.Title, web.Url));
- }
- }
- private static SecureString GetPasswordFromConsoleInput()
- {
- ConsoleKeyInfo info;
- //Get the user's password as a SecureString
- SecureString securePassword = new SecureString();
- do
- {
- info = Console.ReadKey(true);
- if (info.Key != ConsoleKey.Enter)
- {
- securePassword.AppendChar(info.KeyChar);
- }
- }
- while (info.Key != ConsoleKey.Enter);
- return securePassword;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement