SPGroup Class
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

SPGroup Class

Windows SharePoint Services 3

Represents a group on a Windows SharePoint Services Web site.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class SPGroup : SPPrincipal

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 Users, Groups, and Authorization.

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

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";

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

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