Karine Bosch’s Blog

On SharePoint

WebApplicationSelector


Introduction

The WebApplicationSelector control allows a user to select one of the available web applications in the farm.

 webapplicationselector

A user can select another web applicationby clicking the arrow at the right of the control.

webapplicationselector-change1

This opens a popup where the user can select one of the available web applications.

webapplicationselector-popup

 

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

7 Comments »

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

    Comment by Paul Matthews | April 28, 2009 | Reply

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

    Comment by Karine | April 28, 2009 | Reply

  3. 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);
    }
    }
    }

    Comment by Paul Matthews | April 30, 2009 | Reply

  4. I wanted to know how this control loads the data. I have a requirement, i want to know which is current web application is selected though out the central administration. How to get this data, is there any web service which is being called or any property of the central administration .. which can give this value.

    I have tried to find out a lot. have not got any clue till now.. Hope you may put some light on it.

    Thanks in advance
    Shobha

    Comment by Shobha | December 30, 2009 | Reply

    • Hi Shobha,
      As already told to you on the forum, we don’t quit understand your question.
      You can create your own custom application pages in Central Admin and place a WebApplicationSelector on the page. In the code behind you can then use the CurrentItem property of the control:

      protected void WebAppSelector_OnContextChange(object sender, EventArgs e)
      {
      Microsoft.SharePoint.Administration.SPWebApplication currentWebApp = WebAppSelector.CurrentItem;
      if (currentWebApp != null)
      DoStuff(currentWebApp);
      }

      Karine

      Comment by Karine Bosch | December 30, 2009 | Reply

  5. Hi Karine,
    great article, helped me a lot. Unfortunatelly I have a very strange problem here and cannot find a solution for a day already.

    When I add two WebApplicationSelector controls on my custom aspx page and then change the selection of one of the controls from the GUI, both event handlers are fired. Short example:

    I have two controls:

    When I change the application in WebAppSelector1, both methods WebAppSelector1_OnContextChange and WebAppSelector2_OnContextChange are executed! The stragest of all is that the sender ID in each handler method is correct!

    Any idea where the problem might be?

    Thanks
    Vasil

    Comment by Vasil | October 11, 2012 | Reply

    • Hi Vasil,
      I have no idea what this could be, I’ve never put two of these controls on the same page. And as it are out of the box controls, you can’t change it. Perhaps looking at the implementation using Reflector can shed a light on it.
      Kind regards,
      Karine

      Comment by Karine Bosch | October 12, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: