Partilhar via


SPGroup Class

Represents a group on a SharePoint Foundation Web site.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPMember
    Microsoft.SharePoint.SPPrincipal
      Microsoft.SharePoint.SPGroup

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<SubsetCallableTypeAttribute> _
Public NotInheritable Class SPGroup _
    Inherits SPPrincipal
'Usage
Dim instance As SPGroup
[SubsetCallableTypeAttribute]
public sealed class SPGroup : SPPrincipal

Remarks

Use the Groups property of the SPUser or SPWeb class to return the collection of groups for the user or Web site. Otherwise, use the OwnedGroups property of the SPUser class to return the collection of groups owned by a user, or the SiteGroups property of the SPWeb class to return all the groups in the site collection.

Use an indexer to return a single group from the collection. For example, if the collection is assigned to a variable named collGroups, use myGroups[index] in Microsoft C#, or myGroups(index) in Microsoft Visual Basic, where index is either the index number of the group in the collection or the name of the group.

Every group can be represented by an SPMember object and has a unique member identifier (see ID property). The following example assigns a group to an SPMember object:

SPMember oMember = oWebsite.SiteGroups["Cross-Site_Group_Name"];

For general information about groups and security, see Authorization, Users, and Groups.

Examples

The following code example changes the name, owner, and description of a group in a site collection.

Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
Try
    Dim myGroup As SPGroup = webSite.SiteGroups("Original_Name")
            
    myGroup.Name = "New_Name"
    myGroup.Owner = webSite.Users("Domain_Name\User")
    myGroup.Description = "Description"
            
    myGroup.Update()
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsite = SPContext.Current.Site.RootWeb)
{
    SPGroup oGroup = oWebsite.SiteGroups["Original_Name"];
    oGroup.Name = "New_Name";
    oGroup.Owner = oWebsite.Users["Domain_Name\\User"];
    oGroup.Description = "Description";
    oGroup.Update();
}

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPGroup Members

Microsoft.SharePoint Namespace