This topic has not yet been rated - Rate this topic

SPWebApplication.Sites Property

Gets the collection of all site collections in the Web application.

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
public SPSiteCollection Sites { get; }

Property Value

Type: Microsoft.SharePoint.Administration.SPSiteCollection
An SPSiteCollection object that represents the collection of site collections.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
SPWebApplication.Sites And LINQ

When using the SPWebApplication.Sites property it is important to realize that with the introduction of LINQ with SharePoint, you no longer have to Cast<T> (types) to achieve IQueryable support.

So, instead of this example SharePoint Feature activation code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
if (site != null)
{
SPWebApplication webApp = site.WebApplication;
string targetUrl = string.Empty;
foreach (SPSite rootSiteSearch in webApp.Sites.Cast<SPSite>().Where
(rootSiteSearch => rootSiteSearch.RootWeb.Title == "Website Example"))
{
targetUrl = rootSiteSearch.RootWeb.Url;
}
Uri x = new Uri(targetUrl + Constants.CustomizedMasterUrl);
SPWeb rootWeb = site.RootWeb;
}
}

can instead by written as:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
if (site != null)
{
SPWebApplication webApp = site.WebApplication;
string targetUrl = string.Empty;
foreach (SPSite rootSiteSearch in webApp.Sites.Where
(rootSiteSearch => rootSiteSearch.RootWeb.Title == "Website Example"))
{
targetUrl = rootSiteSearch.RootWeb.Url;
}
Uri x = new Uri(targetUrl + Constants.CustomizedMasterUrl);
SPWeb rootWeb = site.RootWeb;
}
}

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com