Export (0) Print
Expand All

SPGroupCollection Class

Represents a collection of SPGroup objects.

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

[SubsetCallableTypeAttribute]
[ClientCallableTypeAttribute(Name = "GroupCollection", ServerTypeId = "{0b9f0e6c-2c15-425e-b0b2-961f78bf1ecf}", 
	CollectionChildItemType = typeof(SPGroup))]
public class SPGroupCollection : SPMemberCollection

Use the Groups property of the SPUser class or the 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 groups owned by a user, or the SiteGroups property of the SPWeb class to return all the groups in the site collection.

To create a group in the site collection, use the Add method.

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

The following code example iterates through all the groups in a site collection and deletes all groups that are owned by the specified user.

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
    SPUserCollection collUsers = oWebsiteRoot.Users;
    SPUser oUser = oWebsiteRoot.Users["User_Name"];

    SPGroupCollection collGroupsWebsite = oWebsiteRoot.SiteGroups;
    SPGroupCollection collGroupsUser = oUser.OwnedGroups;

    foreach (SPGroup oUserGroup in collGroupsUser)
    {
        collGroupsWebsite.RemoveByID(oUserGroup.ID);
    }
}
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.

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

Community Additions

ADD
Show:
© 2014 Microsoft