WebApplicationSelector Class (Microsoft.SharePoint.WebControls)
WebApplicationSelector Class (Microsoft.SharePoint.WebControls)

Namespace: Microsoft.SharePoint.WebControls
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

'Usage

Dim instance As WebApplicationSelector


'Declaration

<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
Public Class WebApplicationSelector
    Inherits PersistedObjectContextSelector(Of SPWebApplication)
Inheritance Hierarchy

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       Microsoft.SharePoint.WebControls.ContextSelector
         Microsoft.SharePoint.WebControls.PersistedObjectContextSelector
          Microsoft.SharePoint.WebControls.WebApplicationSelector
Thread Safety

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

Community Content

WebApplicationSelector (Part 1)
Added by:Thomas Lee

Description

The Microsoft.SharePoint.WebControls.WebApplicationSelector class inherits from Microsoft.SharePoint.WebControls.PersistedObjectContextSelector, providing a way to switch between all Web applications using a pop up window. You can place this Web control in the PlaceHolderPageDescription place holder of your page and surround it with ToolBar and Template_RightButtons tags to create a toolbar that has this control rendered as a drop down list located on the right side.

You can control the appearance and behavior of the WebApplicationSelector control by setting properties defined in this class. For example, AllowAdministrationWebApplication set to true specifies that Central administration site should also appear in the available Web applications. By setting AllowPopup property to false, instead of getting out of the box SelectWebApplication.aspx page in a pop up window, you will be transferred to the actual page passing the required information in query string such as Mode, WebApplicationId, ReturnSelectionPage and AllowAdministrationWebApplication if it is false.

The Microsoft.SharePoint.WebControls.WebApplicationSelector class exposes a ContextChange event which can be used to control the behavior of the control when user changes the context. This control is mostly used in custom Web parts and Web part pages targeted at Central administration Web application or any other sites meant to be used for administrative purposes. End users typically don’t need to see all Web applications.

The Usage Scenario

The following code samples show how you might use the WebApplicationSelector control in a custom Web part page (placed in %WSS System Directory%\12\TEMPLATE\ADMIN) named SitesCount.aspx to show the number of SPWeb objects in the given Web application in a label placed in the main content place holder of the page..

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint.WebControls, Microsoft.SharePoint.Administration and System.Web.UI.WebControls namespace.

SitesCount.aspx

<!--[...Ommitted as community content has number of characters limitation.For complete code see below.]--> 
<wssuc:ToolBar id="onetidNavNodesTB" runat="server"> 
<Template_RightButtons>
<SharePoint:WebApplicationSelector id="Selector" runat="server" OnContextChange="Selector_OnContextChange" AllowAdministrationWebApplication="true" AllowPopup="false" />
</Template_RightButtons>
</wssuc:ToolBar>
<!--[...Ommitted as community content has number of characters limitation.For complete code see below.]--> 
WebApplicationSelector (Part 2)
Added by:Stanley Roark

C# Code Example (Code Behind)

public class SitesCount : LayoutsPageBase
{
protected Label Message;
protected WebApplicationSelector Selector;
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
}
protected void Selector_OnContextChange(object sender, EventArgs e)
{
int count = 0;
SPWebApplication webApp = Selector.CurrentItem;
for (int i = 0; i < webApp.Sites.Count; i++)
{
using (SPSite site = webApp.Sites[i])
{
count += site.AllWebs.Count;
}
}
Message.Text = string.Format("There are currently <b>{0}</b> webs in the selected Web application.", count.ToString());0
}
}

Visual Basic .NET Code Example (Code Behind)

Public Class SitesCount
Inherits LayoutsPageBase
Protected Message As Label
Protected Selector As WebApplicationSelector
Protected Overloads Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
End Sub
Protected Sub Selector_OnContextChange(ByVal sender As Object, ByVal e As EventArgs)
Dim count As Integer = 0
Dim webApp As SPWebApplication = Selector.CurrentItem
For i As Integer = 0 To webApp.Sites.Count - 1
Using site As SPSite = webApp.Sites(i)
count += site.AllWebs.Count
End Using
Next
Message.Text = String.Format("There are currently <b>{0}</b> webs in the selected Web application.", count.ToString())
End Sub
End Class
WebApplicationSelector (Part 3) :SitesCount.aspx
Added by:Thomas Lee

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" Inherits="SDKBounty.SitesCount,SDKBounty, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e879e7dc8b7c3e9" MasterPageFile="~/_layouts/application.master" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBar" src="~/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
<SharePoint:EncodedLiteral ID="TitleArea" Text="Site Count"EncodeMethod="HtmlEncode" runat="server"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
<wssuc:ToolBar id="onetidNavNodesTB" runat="server">
<Template_RightButtons>
<SharePoint:WebApplicationSelector id="Selector" runat="server" OnContextChange="Selector_OnContextChange" AllowAdministrationWebApplication="true" AllowPopup="false"/>
</Template_RightButtons>
</wssuc:ToolBar>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label ID="Message" class="ms-info" runat="server"></asp:Label>
</asp:Content>

Screenshots
Added by:Reza Alirezaei - MVP

For the screenshots of the content above, please visit : http://blogs.devhorizon.com/reza/?p=645

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View