Export (0) Print
Expand All

SPSiteCollection Class

Windows SharePoint Services 3

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

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

[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.

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();
        }
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft