Export (0) Print
Expand All

SPGroup.AddUser method (SPUser)

Adds the specified user to the group.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public void AddUser(
	SPUser user
)

Parameters

user
Type: Microsoft.SharePoint.SPUser

The user to be added to the group.

This method calls AddUser(String, String, String, String) method with the values of the LoginName, Email, Name, and Notes properties of user to add the user to the group.

The following code example adds all the users of a subsite to a group in the site collection.

using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Web_Site_Name"])
{
    SPGroup oGroup = oWebsite.SiteGroups["Group_Name"];
    SPUserCollection collUsers = oWebsite.Users;

    foreach (SPUser oUser in collUsers)
    {
oGroup.AddUser(oUser);
    }
}
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Show:
© 2014 Microsoft