Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (8)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPSiteCollection Class (Microsoft.SharePoint.Administration)
Represents a collection of SPSite objects or site collections on a virtual server.

Namespace: Microsoft.SharePoint.Administration
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<DefaultMemberAttribute("Item")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
Public Class SPSiteCollection
    Inherits SPBaseCollection
    Implements ICollection
Visual Basic (Usage)
Dim instance As SPSiteCollection
C#
[DefaultMemberAttribute("Item")] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
public class SPSiteCollection : SPBaseCollection, ICollection

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.

Visual Basic
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
C#
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();
        }
    }
}
System.Object
   Microsoft.SharePoint.Administration.SPAutoSerializingObject
     Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.Administration.SPSiteCollection
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker