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.
Microsoft.SharePoint.Administration.SPAutoSerializingObject
Microsoft.SharePoint.SPBaseCollection
Microsoft.SharePoint.Administration.SPSiteCollection
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
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.
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(); } } }