SPGroupCollection.Add method
Adds a group to the collection of groups in a site collection.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'Declaration
Public Sub Add ( _
name As String, _
owner As SPMember, _
defaultUser As SPUser, _
description As String _
)
'Usage
Dim instance As SPGroupCollection
Dim name As String
Dim owner As SPMember
Dim defaultUser As SPUser
Dim description As String
instance.Add(name, owner, defaultUser, _
description)
public void Add(
string name,
SPMember owner,
SPUser defaultUser,
string description
)
Parameters
name
Type: System.StringA string that represents the new group name.
owner
Type: Microsoft.SharePoint.SPMemberAn SPMember object that specifies the owner.
defaultUser
Type: Microsoft.SharePoint.SPUserAn SPUser object that specifies the default user for the group.
description
Type: System.StringA string that contains a description for the group.
Exceptions
Exception | Condition |
---|---|
ArgumentException | owner is null . |
SPException | The group collection is read-only. -or- The owner is a role or domain group. -or- The group is not a site-level group. |
Examples
The following code example creates a group in the current site collection.
Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
Try
Dim groups As SPGroupCollection = webSite.SiteGroups
Dim user As SPUser = webSite.Users("User_Name")
Dim member As SPMember = webSite.Users("User_Name")
groups.Add("Group_Name", member, user, "Description")
Finally
webSite.Dispose()
End Try
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
SPGroupCollection collGroups = oWebsiteRoot.SiteGroups;
SPUser oUser = oWebsiteRoot.Users["User_Name"];
SPMember oMember = oWebsiteRoot.Users["User_Name"];
collGroups.Add("Group_Name", oMember, oUser, "Description");
}
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.