Get all SharePoint site users using Client object model

  • Posted by: Softree Technology

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;

ctx.Load(web.RoleAssignments,

roles => roles.Include(

r => r.Member,

r => r.Member.LoginName,

r => r.Member.Title

));

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;

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

{

userLoginName = roleAssign.Member.LoginName;

userTitle = roleAssign.Member.Title;

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

}

}

catch { }

}

Console.ReadLine();

}

catch { }

}

}

}

Author: Softree Technology

Leave a Reply