SPGroup.RemoveUser Method
Removes the specified user from the group.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Syntax
'Declaration
Public Sub RemoveUser ( _
user As SPUser _
)
'Usage
Dim instance As SPGroup
Dim user As SPUser
instance.RemoveUser(user)
public void RemoveUser(
SPUser user
)
Parameters
user
Type: Microsoft.SharePoint.SPUserThe user to be removed from the group.
Remarks
This method removes the user from the group as specified by the Users property.
Examples
The following code example removes a specified user from every group in a site collection.
Dim site As SPWeb = SPContext.Current.Site.RootWeb
Try
Dim myGroups As SPGroupCollection = site.SiteGroups
Dim user As SPUser = site.Users("User_Name")
Dim group As SPGroup
For Each group In myGroups
group.RemoveUser(user)
Next group
Finally
site.Dispose()
End Try
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
SPGroupCollection collGroups = oWebsiteRoot.SiteGroups;
SPUser oUser = oWebsiteRoot.Users["User_Name"];
foreach (SPGroup oGroup in collGroups)
{
oGroup.RemoveUser(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.