Get all users and groups client object model sharepoint 2010

The code below shows you how to query users and groups using .net client object model. To list users with Javascript client Object Model see our Post Related Post : Retrieve all user in SharePoint 2010 site using Javascript client object model.

The client OM includes the GroupCollection,Group,UserCollection,and User objects to make working with users and groups easier.The client OM also has access to built – in groups such as the owners, members, and visitors groups. You can
access these using the AssociatedOwnerGroup,AssociatedMemberGroup,and AssociatedVisitorGroup properties, which return a Group object.

function GetUsersGroups()
{

ClientContext context = new Microsoft.SharePoint.Client.ClientContext(“http://SPSite”);

GroupCollection groupCollection = context.Web.SiteGroups;
context.Load(groupCollection,
groups = > groups.Include(
group = > group.Users));

context.ExecuteQuery();

foreach (Group group in groupCollection)
{
UserCollection userCollection = group.Users;

foreach (User user in userCollection)
{
MessageBox.Show(“User Name: ” + user.Title + ” Email: ” +
user.Email + ” Login: ” + user.LoginName);
}
}
//Iterate the owners group
Group ownerGroup = context.Web.AssociatedOwnerGroup;
context.Load(ownerGroup);
context.Load(ownerGroup.Users);
context.ExecuteQuery();
foreach (User ownerUser in ownerGroup.Users)
{
MessageBox.Show(“User Name: ” + ownerUser.Title + ” Email: ” +
ownerUser.Email + ” Login: ” + ownerUser.LoginName);
}
context.Dispose();
}
Related Post : Retrieve all user in SharePoint 2010 site using Javascript client object model

Related Posts

Disclaimer: The contents of this site are provided “AS IS”. Information and views expressed including URL(s) and other Internet website references, may change without notice. You bear the risk of using it. SharePoint, MOSS, SharePoint 2010, SharePoint 2013 and Microsoft are trademarks of the Microsoft Corporation.Learning SharePoint only provides Tips,Tricks and Tutorials for using Microsoft SharePoint, Microsoft Office and related Softwares.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.