다음을 통해 공유


SPSiteCollection Class

Represents a collection of SPSite objects or site collections that are associated with a particular Web application, including a top-level Web site and all its sub-sites. Each SPSite object, or site collection, is represented within an SPSiteCollection objects that consists of the collection of all site collections in the Web application.

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)
Available in Sandboxed Solutions: No

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public Class SPSiteCollection _
    Inherits SPBaseCollection _
    Implements IEnumerable(Of SPSite), ICollection,  _
    IEnumerable
'Usage
Dim instance As SPSiteCollection
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPSiteCollection : SPBaseCollection, 
    IEnumerable<SPSite>, ICollection, IEnumerable

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