Pages

Tuesday, January 22, 2013

Determine which SharePoint Web Front End Server you are hitting

Often, we need to identify which WFE server on SharePoint we are hitting when the farm has load balancing.

I decided to create a very simple feature that would display the server name by overriding the GlobalNavigation delegate control of each page that was being accessed across every site collection and sub sites in a web application.

When accessing any page, you will see the server name similar to below:




First, I created the user control, and added one simple label:

<asp:Label ID="lblServerName" runat="server" BackColor="Green" ForeColor="White"></asp:Label>

In the code behind, I put code to check whether the current user is a farm administrator, or if the query parameter, "showserver" has been passed into the url. If either are true, then I display the name of the sharepoint server on the label:

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsFarmAdmin() || ServerParamIsSet())
                {
                    this.lblServerName.Width = new Unit("100%");
                    this.lblServerName.Text = string.Format("{0}", Page.Server.MachineName);
                }
            }
            catch { }
        }



Next, I added an Elements manifest, and specified the path to where the control template will be deployed to on the server, a sequence number lower than 100, and specified it to place the user control in the GlobalNavigation delegate control placeholder.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="GlobalNavigation" Sequence="90" ControlSrc="~/_ControlTemplates/SPServerName/SPServerName.ascx" />
</Elements>


Finally, I created a feature and set it to deploy to the web application.


Installation instructions:
1. Download the wsp I uploaded to http://spservername.codeplex.com/
2. Deploy the solution to your farm
3. Go to the web application features, and enable the SPServerName feature


Notes:
  • There are no changes made to any master page and the feature can easily be turned on and off
  • By default, Farm administrators will always see the server name at the top of the page.
  • Non-farm administrators can also display the server name if they pass in the query parameter "showserver=1"
Ex:

http://myportal.com/siteabc?showserver=1

 

1 comment: