Get all SharePoint site users using Client object model

  • Posted by: Softree Technology

[code lang=”c”]
class Program

{

static void Main(string[] args)

{

using (ClientContext ctx = new ClientContext(“http://siteUrl”))

{

NetworkCredential netCredentials = new NetworkCredential(“userName”, “password”);

ctx.Credentials = netCredentials;

try

{

Web web = null;

web = ctx.Web;

RoleAssignmentCollection assignColl;

RoleAssignment roleAssign;

RoleDefinitionBindingCollection bindingColl;

ctx.Load(web.RoleAssignments,

roles => roles.Include(

r => r.Member,

r => r.Member.LoginName,

r => r.Member.Title,

r => r.RoleDefinitionBindings

));

ctx.ExecuteQuery();

assignColl = web.RoleAssignments;

for (int isitePermCount = 0; isitePermCount < assignColl.Count; isitePermCount++)

{

try

{

roleAssign = assignColl[isitePermCount];

string userLoginName = string.Empty;

string userTitle = string.Empty;

string permissionLevelColl = string.Empty;

if (roleAssign.Member.PrincipalType.ToString() == “User” || roleAssign.Member.PrincipalType.ToString() == “DomainGroup”|| roleAssign.Member.PrincipalType.ToString() == “SecurityGroup”)

{

userLoginName = roleAssign.Member.LoginName;

userTitle = roleAssign.Member.Title;

bindingColl = roleAssign.RoleDefinitionBindings;

foreach (RoleDefinition role in bindingColl)

{

try

{

string permissionLevel = role.Name;

permissionLevelColl = permissionLevelColl + “” + permissionLevel + “; “;

}

catch { }

}

if (!string.IsNullOrEmpty(permissionLevelColl))

{

permissionLevelColl = permissionLevelColl.TrimEnd().TrimEnd(‘;’);

}

Console.WriteLine(string.Format(“User LoginName:{0}, User Title:{1}, Permissions:{2}”, userLoginName, userTitle, permissionLevelColl));

}

}

catch { }

}

Console.ReadLine();

}

catch { }

}

}

}
[/code]

Author: Softree Technology

Leave a Reply

1 Comment