WebApplicationSelector
Introduction
The WebApplicationSelector control allows a user to select one of the available web applications in the farm.
![]()
A user can select another web applicationby clicking the arrow at the right of the control.
![]()
This opens a popup where the user can select one of the available web applications.

Syntax
The WebApplicationSelector control is part of the Microsoft.SharePoint.dll and is located in the Microsoft.SharePoint.WebControls namespace. This control can best be used in custom application pages that are deployed to the SharePoint Central Administration. If you use this control on a normal application page, it generates an exception when the page is rendered.
You can place this control on a custom application page in a ToolBar control within the <Template_RightButtons> element, or you can use it in an InputFormSection control in the <Template_Control> element. The syntax is as follows:
<spuc:WebApplicationSelector runat="server" ID="WebAppSelector" OnContextChange="WebAppSelector_OnContextChange" />
The page directive at the top of the application page is:
<%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
But you can also create it completely from scratch in code:
private WebApplicationSelector WebAppSelector;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
WebAppSelector = new WebApplicationSelector();
WebAppSelector.ID = "MyAppSelector";
WebAppSelector.ContextChange += new EventHandler(WebAppSelector_OnContextChange);
}
Properties
There are a number of properties/attributes you can set to configure the functionality of the WebApplicationSelector:
- AllowAdministrationWebApplication: setting this property to true instructs the control to also include the web application of the Central Administration into the Web Application popup.
- AllowChange: Setting this property to false prevents users from changing the web application.
- AllowPopup: The default value is true and shows the SelectWebApplication.aspx page in a popup window. Setting this property to false forces SharePoint to redirect you to that page.
- CurrentItem: this property contains the selected web application.
- CurrentId: contains the ID of the selected web application.
The above list is not complete. You can find the complete list on MSDN.
Event
The WebApplicationSelector control exposes a very important event: the ContextChange event. This event is triggered when the user changes the selected web application.
protected void WebAppSelector_OnContextChange(object sender, EventArgs e)
{
Microsoft.SharePoint.Administration.SPWebApplication currentWebApp = WebAppSelector.CurrentItem;
if (currentWebApp != null)
DoStuff(currentWebApp);
}

From what you have stated above “This control can best be used in custom application pages that are deployed to the SharePoint Central Administration. If you use this control on a normal application page, it generates an exception when the page is rendered.”
Is there a way around the exception it generates? It is a perfect tool that I would like to use within a _layouts (LayoutsPageBase) page.
Hi Paul,
I’m affraid there is no work around, anyway not that I am aware of. I tried to work around it for an hour or so. If you come to a solution, please feel free to let me know. I’ll post your solution and give you the credits for it.
Karine
Hi Karine,
Thank you for answering. My work around was using a standard ASP.NET dropdown list and populating it with the following code. It replicated enough functionality for me for a user just to select a web application.
try
{
foreach (SPWebApplication wa in SPWebService.ContentService.WebApplications)
{
foreach (SPSite sc in wa.Sites)
{
//Response.Write(SPEncode.HtmlEncode(sc.WebApplication.Name) + ” :: ” +
// SPEncode.HtmlEncode(sc.Url) + “”);
item = new ListItem(sc.WebApplication.Name, sc.Url);
ddWebs.Items.Add(item);
}
}
}