User Tools

Site Tools


notes:sharepoint2010:spsite

Site Collections (SPSite) in SharePoint 2010

Enumerate site collections from a single web application:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;
...
SPWebApplication webApp = SPContext.Current.Site.WebApplication;
 
// SPWebApplication.Sites returns SPSiteCollection - a collection of site collections
foreach (SPSite site in webApp.Sites) 
{
    try
    {
    	// Site collections do not have names. Use a part of URL as a site collection name.
        string url = site.Url.Replace("http://", "");
        string name = (url.LastIndexOf("/") == -1 ? "Default Site" : url.Substring(url.LastIndexOf("/") + 1));
 
        // Display the site collection's url and name.
        Debug.WriteLine(url);
        Debug.WriteLine(name);
    }
    finally
    {
        if (site != null)
            site.Dispose();
    }
}

Populate a drop-down list with all site collections available in the farm:

Site collections:
<br />
<!-- Selecting a site collection from the ddlSites drop-down list displays a list of all websites (SPWebs) 
     belonging to this site collection -->
<asp:DropDownList ID="ddlSites" Width="200" OnSelectedIndexChanged="ddlSites_SelectedIndexChanged" 
    AutoPostBack="true" runat="server"></asp:DropDownList><br />
<br />
Web sites:
<br />
<!-- Selecting a website from the lstWebs list displays its URL -->
<asp:ListBox ID="lstWebs" Rows="8" Width="200" OnSelectedIndexChanged="lstWebs_SelectedIndexChanged" 
    AutoPostBack="true" runat="server" /><br />
<asp:Label ID="lblWebUrl" runat="server" />
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
 
// Solve a namespace clash.
using WebListItem = System.Web.UI.WebControls.ListItem;
 
namespace WBS.SharePointTest
{
    public partial class TestPage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    // Iterate through all web applications in the farm.
                    SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>("");
                    foreach (SPWebApplication webapp in service.WebApplications)
                    {
                        // Iterate through all site collections in each web application.
                        foreach (SPSite site in webapp.Sites)
                        {
                            ddlSites.Items.Add(site.Url);
                        }
                    }
                });
            }
        }
 
        protected void ddlSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Determine the drop-down list object.
            DropDownList ddlSites = (DropDownList)sender;
 
            if (ddlSites.SelectedIndex != -1 && ddlSites.SelectedItem.ToString() != "")
            {
                string siteUrl = ddlSites.SelectedItem.ToString();
                PopulateWebs(siteUrl);
            }
        }
 
        protected void lstWebs_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Determine the listbox object.
            ListBox lstWebs = (ListBox)sender;
            lblWebUrl.Text = lstWebs.SelectedValue;
        }
 
        private void PopulateWebs(string siteUrl)
        {
            lstWebs.Items.Clear();
 
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // Create a site collection object for the specified site URL.
                using (SPSite site = new SPSite(siteUrl))
                {
                    // Iterate through all the websites in the site collection.
                    foreach (SPWeb web in site.AllWebs)
                    {
                        try
                        {
                            WebListItem item = new WebListItem(web.Name, web.Url);
                            lstWebs.Items.Add(item);
                        }
                        finally
                        {
                            if (web != null)
                                web.Dispose();
                        }
                    }
                }
            });
        }
    }
}

This is a sample output. A user selected the default site collection in the 556 web app and the TestingUrl website:

Obtain an email of the site collection admin:

using (SPSite site = new SPSite(SPContext.Current.Web.Site.Url))
{
    string senderEmail = site.WebApplication.OutboundMailSenderAddress;
    // ...
}
notes/sharepoint2010/spsite.txt · Last modified: 2016/10/19 by admin