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)

public void Add(
	string name,
	SPMember owner,
	SPUser defaultUser,
	string description
)

Parameters

name
Type: System.String

A string that represents the new group name.

owner
Type: Microsoft.SharePoint.SPMember

An SPMember object that specifies the owner.

defaultUser
Type: Microsoft.SharePoint.SPUser

An SPUser object that specifies the default user for the group.

description
Type: System.String

A string that contains a description for the group.

ExceptionCondition
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.

The following code example creates a group in the current site collection.

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