How to: Work with Users and Groups
Applies to: SharePoint Foundation 2010
Available in SharePoint Online
The same data retrieval logic that applies to Web sites, lists, and list items in the Microsoft.SharePoint.Client namespace applies to users and groups. Like other objects in this namespace, you can use a CreationInformation object to create a user or group object.
Retrieving all users from a SharePoint group
The SiteGroups property of the Web class gets all groups in all Web sites within a site collection. As shown in the following example, you can use the GetById(Int32) method to return a specific group from the collection of groups. The Users property of the Group class gets all the users in the group. Because the following example loads the users in the specified group, all properties of each user object are available.
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveAllUsersInGroup
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
GroupCollection collGroup = clientContext.Web.SiteGroups;
Group oGroup = collGroup.GetById(7);
UserCollection collUser = oGroup.Users;
clientContext.Load(collUser);
clientContext.ExecuteQuery();
foreach (User oUser in collUser)
{
Console.WriteLine("User: {0} ID: {1} Email: {2} Login Name: {3}",
oUser.Title, oUser.Id, oUser.Email, oUser.LoginName);
}
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class RetrieveAllUsersInGroup
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
Dim oGroup As Group = collGroup.GetById(7)
Dim collUser As UserCollection = oGroup.Users
clientContext.Load(collUser)
clientContext.ExecuteQuery()
Dim oUser As User
For Each oUser In collUser
Console.WriteLine("User: {0} ID: {1} Email: {2} Login Name: {3}", _
oUser.Title, oUser.Id, oUser.Email, oUser.LoginName)
Next oUser
End Sub
End Class
End Namespace
Retrieving specific properties of users
The following example modifies the previous example to return only the title and e-mail address of each user in a specific group. The example uses the Include<TSource>(IQueryable<TSource>, []) method to specify that only the Title and Email properties are available on each user object, and calling other properties returns a PropertyOrFieldNotInitializedException.
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveSpecificUserProperties
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
GroupCollection collGroup = clientContext.Web.SiteGroups;
Group oGroup = collGroup.GetById(7);
UserCollection collUser = oGroup.Users;
clientContext.Load(collUser,
users => users.Include(
user => user.Title,
user => user.LoginName,
user => user.Email));
clientContext.ExecuteQuery();
foreach (User oUser in collUser)
{
Console.WriteLine("User: {0} Login name: {1} Email: {2}",
oUser.Title, oUser.LoginName, oUser.Email);
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class RetrieveSpecificUserProperties
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
Dim oGroup As Group = collGroup.GetById(7)
Dim collUser As UserCollection = oGroup.Users
clientContext.Load(collUser, Function(users) users.Include( _
Function(user) user.Title, _
Function(user) user.LoginName, _
Function(user) user.Email))
clientContext.ExecuteQuery()
Dim oUser As User
For Each oUser In collUser
Console.WriteLine("User: {0} Login name: {1} Email: {2}", _
oUser.Title, oUser.LoginName, oUser.Email)
Next oUser
End Sub
End Class
End Namespace
Retrieving all users in all groups of a site collection
The previous examples show how to return all users from a specified group. To return all users from all groups within a site collection, you can use the Load<T>(T, []) method twice to load both the collection of groups and the collection of users in each group, in order to make all properties of each user and group available. Use LINQ query method syntax to include each user collection of each group, as in the following example.
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveAllUsersAllGroups
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
GroupCollection collGroup = clientContext.Web.SiteGroups;
clientContext.Load(collGroup);
clientContext.Load(collGroup,
groups => groups.Include(
group => group.Users));
clientContext.ExecuteQuery();
foreach (Group oGroup in collGroup)
{
UserCollection collUser = oGroup.Users;
foreach (User oUser in collUser)
{
Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}",
oGroup.Id, oGroup.Title, oUser.Title, oUser.LoginName);
}
}
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class RetrieveAllUsersAllGroups
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
clientContext.Load(collGroup)
clientContext.Load(collGroup, Function(groups) groups.Include( _
Function(group) group.Users))
clientContext.ExecuteQuery()
Dim oGroup As Group
For Each oGroup In collGroup
Dim collUser As UserCollection = oGroup.Users
Dim oUser As User
For Each oUser In collUser
Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}", _
oGroup.Id, oGroup.Title, oUser.Title, oUser.LoginName)
Next oUser
Next oGroup
End Sub
End Class
End Namespace
To improve efficiency, you can call the Load<T>(T, []) method once and modify the query expression so that it includes only specific properties, as in the following example.
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveAllUsersAllGroupsSpecificProperties
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
GroupCollection collGroup = clientContext.Web.SiteGroups;
clientContext.Load(collGroup,
groups => groups.Include(
group => group.Title,
group => group.Id,
group => group.Users.Include(
user => user.Title,
user => user.LoginName)));
clientContext.ExecuteQuery();
foreach (Group oGroup in collGroup)
{
UserCollection collUser = oGroup.Users;
foreach (User oUser in oGroup.Users)
{
Console.WriteLine("Group: {0} Group ID: {1} User: {2} Login Name: {3}",
oGroup.Title, oGroup.Id, oUser.Title, oUser.LoginName);
}
}
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class RetrieveAllUsersAllGroupsSpecificProperties
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
clientContext.Load(collGroup, Function(groups) groups.Include( _
Function(group) group.Title, _
Function(group) group.Id, _
Function(group) group.Users.Include( _
Function(user) user.Title, _
Function(user) user.LoginName)))
clientContext.ExecuteQuery()
Dim oGroup As Group
For Each oGroup In collGroup
Dim collUser As UserCollection = oGroup.Users
Dim oUser As User
For Each oUser In oGroup.Users
Console.WriteLine("Group: {0} Group ID: {1} User: {2} Login Name: {3}", _
oGroup.Title, oGroup.Id, oUser.Title, oUser.LoginName)
Next oUser
Next oGroup
End Sub
End Class
End Namespace
Adding a user to a SharePoint group
If the user object you want to add to a group already exists within the site collection, you can use the AddUser(User) method to add the user to the user collection of the group. But if the user does not already exist, as in the following code example, use the UserCreationInformation class to define a new user, and then add the new user to the group's user collection through the Add(UserCreationInformation) method. This example uses the GetById(Int32) method to return a specific group from the collection of groups within the site collection, then defines a new user object, and adds it to the user collection of the group.
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class AddUserToSharePointGroup
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection ");
GroupCollection collGroup = clientContext.Web.SiteGroups;
Group oGroup = collGroup.GetById(6);
UserCreationInformation userCreationInfo = new UserCreationInformation();
userCreationInfo.Email = "alias@somewhere.com";
userCreationInfo.LoginName = @"DOMAIN\alias";
userCreationInfo.Title = "John";
User oUser = oGroup.Users.Add(userCreationInfo);
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class AddUserToSharePointGroup
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
Dim oGroup As Group = collGroup.GetById(7)
Dim userCreationInfo As New UserCreationInformation()
userCreationInfo.Email = "alias@somewhere.com"
userCreationInfo.LoginName = "DOMAIN\alias"
userCreationInfo.Title = "John"
Dim oUser As User = oGroup.Users.Add(userCreationInfo)
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
For information and examples about working with client objects within the context of the Microsoft SharePoint Foundation 2010 Silverlight object model, see Using the Silverlight Object Model.
See Also
Concepts
How to: Break Role Assignment Inheritance
Authorization, Users, and Groups
SharePoint Client Object Creation
SharePoint 2010 Client Object Model Guidelines
Common Programming Tasks in the Managed Client Object Model