共用方式為


SPSiteCollection Class

Represents a collection of SPSite objects or site collections on a virtual server.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.Administration.SPSiteCollection

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

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPSiteCollection _
    Inherits SPBaseCollection _
    Implements ICollection

Dim instance As SPSiteCollection
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPSiteCollection : SPBaseCollection, 
    ICollection

Remarks

Use the Sites property of the SPWebApplication class to return a collection of SPSite objects that represent all the site collections for a Web application. To create a site collection, use the Add method.

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

Examples

The following example iterates through all site collections within the current Web application to add an item to the top-level Announcements list for each member that has been added to a group.

Dim webApp As SPWebApplication = SPContext.Current.Site.WebApplication
Dim siteCollections As SPSiteCollection = webApp.Sites

Dim siteCollection As SPSite

For Each siteCollection In  siteCollections
    Dim changes As SPChangeCollection = siteCollection.GetChanges()
    Dim change As SPChange

    For Each change In  changes
        If change.ChangeType = SPChangeType.MemberAdd Then
            Dim webSite As SPWeb = siteCollection.OpenWeb()
            Dim groups As SPGroupCollection = webSite.Groups
            Dim list As SPList = webSite.GetList("Lists/Announcements")
            Dim items As SPListItemCollection = list.Items
            Dim group As SPChangeGroup = CType(change, SPChangeGroup)

            Dim item As SPListItem = items.Add()
            item("Title") = "User added to " + 
                groups.GetByID(group.Id).Name + " on " + 
                change.Time.ToString()
            item.Update()
        End If
    Next change
Next siteCollection
SPWebApplication webApp = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApp.Sites;

foreach (SPSite siteCollection in siteCollections)
{
    SPChangeCollection changes = siteCollection.GetChanges();

    foreach (SPChange change in changes)
    {
        if (change.ChangeType == SPChangeType.MemberAdd)
        {
            SPWeb webSite = siteCollection.OpenWeb();
            SPGroupCollection groups = webSite.Groups;
            SPList list = webSite.GetList("Lists/Announcements");
            SPListItemCollection items = list.Items;

            SPChangeGroup group = (SPChangeGroup)change;

            SPListItem item = items.Add();
                    item["Title"] = "User added to " + 
                    groups.GetByID(group.Id).Name + " on " +                     change.Time.ToString();
            item.Update();
        }
    }
}

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

SPSiteCollection Members

Microsoft.SharePoint.Administration Namespace