Microsoft.SharePoint.WebCon ...


WebApplicationSelector Class (Microsoft.SharePoint.WebControls)

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

Visual Basic (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)
Visual Basic (Usage)
Dim instance As WebApplicationSelector
C#
[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 : PersistedObjectContextSelector<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

Tags :


Community Content

Thomas Lee
WebApplicationSelector (Part 1)

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

Stanley Roark
WebApplicationSelector (Part 2)

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

Thomas Lee
WebApplicationSelector (Part 3) :SitesCount.aspx

<%@ 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>


Reza Alirezaei - MVP
Screenshots

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

Tags : screenshots

Page view tracker