Export (0) Print
Expand All

How to: Work with Users and Groups

SharePoint 2010

Last modified: April 29, 2011

Applies to: SharePoint Foundation 2010

In this article
Retrieving all users from a SharePoint group
Retrieving specific properties of users
Retrieving all users in all groups of a site collection
Adding a user to a SharePoint group

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.

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);
            }
        }
    }
}

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);
            }
        }
}

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);
                }
            }  
        }
    }
}

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);
                }
            }
         }
    }
}

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(); 
        }
    }
}

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.

Show:
© 2014 Microsoft