SPGroup.AddUser method (SPUser)
Adds the specified user to the group.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'Declaration
Public Sub AddUser ( _
user As SPUser _
)
'Usage
Dim instance As SPGroup
Dim user As SPUser
instance.AddUser(user)
public void AddUser(
SPUser user
)
Parameters
user
Type: Microsoft.SharePoint.SPUserThe user to be added to the group.
Remarks
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.
Examples
The following code example adds all the users of a subsite to a group in the site collection.
Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("Web_Site_Name")
Try
Dim group As SPGroup = webSite.SiteGroups("Group_Name")
Dim users As SPUserCollection = webSite.Users
Dim user As SPUser
For Each user In users
group.AddUser(user)
Next user
Finally
webSite.Dispose()
End Try
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);
}
}
Note
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.