Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace SharePointOnlineTest {
class Program {
static void Main(string[] args) {
string userName = "xxxxxxx@tenant.sharepoint.com"; //give your username here
string password = "xxxxx"; //give your password
var securePassword = new SecureString();
foreach (char c in password) {
securePassword.AppendChar(c);
}
string siteURL = "https://xxxxxx.sharepoint.com/yoursitehere";
//Create the client context object and set the credentials
ClientContext clientContext = new ClientContext(siteURL);
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
//Load the web
Web web = clientContext.Web;
//get all lists
ListCollection collList = web.Lists;
clientContext.Load(web);
clientContext.Load(collList);
clientContext.Load(collList, wc => wc.Include(w => w.HasUniqueRoleAssignments, w => w.RootFolder.ServerRelativeUrl));
clientContext.ExecuteQuery();
foreach (List oList in collList) {
Console.WriteLine("-- List: {0} permissions:", oList.RootFolder.ServerRelativeUrl);
var listItems = oList.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(listItems);
clientContext.Load(listItems,
items => items.Include(
item => item.Id,
item => item.DisplayName,
item => item.HasUniqueRoleAssignments,
item => item.Folder,
item => item.File,
item => item.ContentType
));
clientContext.ExecuteQuery();
foreach (var listItem in listItems) {
Console.WriteLine("List item: " + listItem["ID"]);
if (listItem.HasUniqueRoleAssignments) {
if (listItem.HasUniqueRoleAssignments) {
clientContext.Load(listItem.RoleAssignments,
ras => ras.Include(
item => item.PrincipalId,
item => item.Member.LoginName,
item => item.Member.Title,
item => item.Member.PrincipalType,
item => item.RoleDefinitionBindings));
clientContext.ExecuteQuery();
if (listItem.RoleAssignments.Count == 0) {
Console.WriteLine("Error: Expected to have unique permissions but couldn't find them!");
continue;
}
foreach (var roleAsg in listItem.RoleAssignments) {
Console.WriteLine("User/Group: " + roleAsg.Member.LoginName);
List<string> roles = new List<string>();
foreach (var role in roleAsg.RoleDefinitionBindings) {
roles.Add(role.Description);
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Permissions: " + string.Join(",", roles.ToArray()));
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("----------------");
}
}
} else {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("No unique permission found");
Console.ForegroundColor = ConsoleColor.White;
}
Console.WriteLine("###############");
}
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment