SPGroupCollection 类

Represents a collection of SPGroup objects.

继承层次结构

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPMemberCollection
        Microsoft.SharePoint.SPGroupCollection

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Class SPGroupCollection _
    Inherits SPMemberCollection
用法
Dim instance As SPGroupCollection
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.

Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
Try
    Dim users As SPUserCollection = webSite.Users
    Dim user As SPUser = webSite.Users("User_Name")
 
    Dim webGroups As SPGroupCollection = webSite.SiteGroups
    Dim userGroups As SPGroupCollection = user.OwnedGroups
 
    Dim userGroup As SPGroup
    For Each userGroup In  userGroups
        webGroups.RemoveByID(userGroup.ID)
    Next userGroup
Finally
    webSite.Dispose()
End Try
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);
    }
}

备注

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.

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPGroupCollection 成员

Microsoft.SharePoint 命名空间