.NET life

HUGON Jérôme
Microsoft Certified Technology Specialist Microsoft Certified Application Developer Microsoft Certified Professional

Using the Developer Dashboard

SharePoint 2010

Category SharePoint 2010  | Publication Date : 3/21/2011

SharePoint 2010 has many new features, among them, the Developer Dashboard. This is a dashboard for analyzing the execution and performance of the current page. The information provided will execution time of queries and events to more general information as the user logged on or page state.

 

By default this feature is not enabled. You can choose between four methods to activate it: a STSADM command, a PowerShell script, a .NET code or the Developer Dashboard configuration feature.

There are three levels of activation:

  • Off: Disabled, this is the default value.
  • On: The feature is available on all pages
  • OnDemand: An icon is displayed on the pages to enable or disable the feature.

The state OnDemand is, in my opinion, the most practical because you can do this only on the pages under development.

 

This operation requires that you be administrator of the SharePoint farm.

 

STSADM

The command line tool, STSADM, is one of the methods for modifying the activation status of the Developer Dashboard. Despite substituting PowerShell, it is still active and functional from the folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN.

The idea is to change the property developer-dashboard by assigning the desired level:

Enabling:

stsadm -o setproperty -pn developer-dashboard -pv On

Disabling:

stsadm -o setproperty -pn developer-dashboard -pv Off

On demand:

stsadm -o setproperty -pn developer-dashboard -pv OnDemand

PowerShell

Since the 2010 release, SharePoint is manageable from PowerShell, a powerful tool that supports the handling of .NET APIs.

This tool aims to become the reference for the server side administration of Microsoft products.

A shortcut, named SharePoint 2010 Management Shell, is available from the server Start menu:

SharePoint 2010 - Shell

Here is the code to execute for updating the display level of the Dashboard:

$srv = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$setting =$srv.DeveloperDashboardSettings
$setting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand
$setting.Update()

You can change OnDemand by the value On or Off regarding your need.

 

.NET code

The last technic for the update is to create a piece of code .NET. You can create a console application to execute the following code:

using Microsoft.SharePoint.Administration;

SPDeveloperDashboardSettings dashboardSettings = SPWebService.ContentService.DeveloperDashboardSettings;
dashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.OnDemand;
dashboardSettings.Update();

You should add a reference to the library Microsoft.SharePoint.dll located in the folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI to your project.

 

The Developer Dashboard configuration feature

This feature is available from the Wictor Wilén website. After downloaded, installed and activated, you can manage the settings of the dashboard and its activation from the menu General Application Settings in the Central Administration:

SharePoint 2010 - Development Settings

SharePoint 2010 - Development Settings - Details

 

More properties

The class SPDeveloperDashboardSettings has properties to refine the configuration.

 

MaximumCriticalEventsToTrack

This is the maximum number of critical events and assertions that will be saved in a query. If there are more critical events that the maximum specified, these will be ignored. This property may be affected with the value 0 to disable tracing assertions.

The following example resumed the previous section Code .NET and change the property MaximumCriticalEventsToTrack to display a maximum of 15 critical events into the dashboard:

dashboardSettings.MaximumCriticalEventsToTrack = 15;

 

MaximumSQLQueriesToTrack

This property represents the maximum number of SQL queries that will be recorded during execution. Queries exceeding the maximum number will not be saved.

The following example resumed the previous section Code .NET  and change the property MaximumSQLQueriesToTrack to display a maximum of 10 SQL queries into the dashboard:

dashboardSettings.MaximumSQLQueriesToTrack = 10;

 

RequiredPermissions

To view information about the dashboard, the default permission is required AddAndCustomizePages. You can change this property by assigning a new value to the property RequiredPermissions using an object of type SPBasePermissions.

The following example resumed the previous section Code .NET  and change the property RequiredPermissions so that the dashboard is visible only by those who have the permission ManageWeb:

dashboard.RequiredPermissions = SPBasePermissions.ManageWeb;

 

 TraceEnabled

This boolean property specifies whether a link to the full trace will be displayed at the bottom of the dashboard whatever if it is launched or not.

The following example resumed the previous section Code .NET  and change the property TraceEnabled to display this link:

dashboardSettings.TraceEnabled= true;

 

The Developer Dashboard visually

If the Developer Dashboard is disabled, there is, of course, no visual change. When activated, the next block appears at the bottom of the page:

SharePoint 2010 - Developer Dashboard 1

The outline of this block is green if the page was executed in a normal manner, yellow to present a warning and red in case of error or critical event.

Where the dashboard is set to appear on demand, an icon is added to the right of the current user and clicking on it displays the dashboard:

SharePoint 2010 - Developer Dashboard 2

By clicking on the links under the sections Database Queries and SPRequest Allocations, you can access the details of requests:

SharePoint 2010 - Developer Dashboard 3

 

Code optimization through Dashboard Developer

To illustrate the use of the dashboard and how to optimize your code, we create the following two pages in the folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\ADMIN: testDeveloperDashboard1.aspx and testDeveloperDashboard2.aspx.

 

testDeveloperDashboard1.aspx

This page includes a method to display a list of records in the Announcements list by taking all elements:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" DynamicMasterPageFile="~masterurl/default.master" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Test Developer Dashboard 1
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Test Developer Dashboard 1
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
result.Text = GetRecords();
}

    public string GetRecords()
    {
        StringBuilder sb = new StringBuilder();
        SPListItemCollection its = SPContext.Current.Web.Lists["Announcements"].Items;
        foreach (SPListItem it in its)
        {
            sb.AppendFormat("{0}<br />", it["Title"]);
        }
        return sb.ToString();
    }
</script>

<asp:Label id="result" runat="server" />
</asp:Content>

 

The execution result of this page to the dashboard is as follows:

SharePoint 2010 - Developer Dashboard 4

Note the execution time of 509.55 ms

 

testDeveloperDashboard2.aspx

This page includes a method to display a list of records of the Announcements list with a CAML query to retrieve only the desired columns:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" DynamicMasterPageFile="~masterurl/default.master" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Test Developer Dashboard 2
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Test Developer Dashboard 2
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
result.Text = GetRecords();
}

    public string GetRecords()
    {
        StringBuilder sb = new StringBuilder();
        SPQuery query = new SPQuery();
        query.ViewFields = "<FieldRef Name=\"Title\" />";
        SPListItemCollection its = SPContext.Current.Web.Lists["Announcements"].GetItems(query);
        foreach (SPListItem it in its)
        {
            sb.AppendFormat("{0}<br />", it["Title"]);
        }
        return sb.ToString();
    }
</script>

<asp:Label id="result" runat="server" />
</asp:Content>

The execution result of this page to the dashboard is as follows:

SharePoint 2010 - Developer Dashboard 5

Note the execution time of 349.10 ms

 

You can make comparisons between different bit of code to optimize it. Of course, we must make an average execution time on several calls from the page to have a value closer to reality.

 

Add event at Developer Dashboard

To illustrate this point, we'll create a new test page testDeveloperDashboard3.aspx whose aim will be to add one of your own event to the dashboard:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" DynamicMasterPageFile="~masterurl/default.master" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>

<
asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Test Developer Dashboard 3
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
Test Developer Dashboard 3
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
using (SPMonitoredScope s = new SPMonitoredScope("GetRecords", 3000, null))
{
result.Text = GetRecords();
}
}

public string GetRecords()
    {
StringBuilder sb = new StringBuilder();
SPQuery query = new SPQuery();
        query.ViewFields = "<FieldRef Name=\"Title\" />";
SPListItemCollection its = SPContext.Current.Web.Lists["Announcements"].GetItems(query);
foreach (SPListItem it in its)
        {
            sb.AppendFormat("{0}<br />", it["Title"]);
        }
return sb.ToString();
    }
</script>

<asp:Label id="result"runat="server"/>
</asp:Content>

You notice that to add your event, we must create a new object of type SPMonitoredScope, whose constructor takes the name of the event, the maximum execution time and an array of objects of type ISPScopedPerformanceMonitor. Then you must run the code in the using clause. You must also import the namespace Microsoft.SharePoint.Utilities.

The execution result of this page to the dashboard is as follows:

SharePoint 2010 - Developer Dashboard 6

 

Throw exceptions in the Developer Dashboard

You can also use the dashboard to display errors of the page. To illustrate this, create a page testDeveloperDashboard4.aspx as follows:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" DynamicMasterPageFile="~masterurl/default.master" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>

<
asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Test Developer Dashboard 4
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
Test Developer Dashboard 4
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
using (SPMonitoredScope s = new SPMonitoredScope("GetRecords", 3000, null))
{
result.Text = GetRecords();
SPCriticalTraceCounter.AddDataToScope(812, "My critical event !", 1, "Just to try.");
}
}

public string GetRecords()
    {
StringBuilder sb = new StringBuilder();
SPQuery query = new SPQuery();
        query.ViewFields = "<FieldRef Name=\"Title\" />";
SPListItemCollection its = SPContext.Current.Web.Lists["Announcements"].GetItems(query);
foreach (SPListItem it in its)
        {
            sb.AppendFormat("{0}<br />", it["Title"]);
        }
return sb.ToString();
    }
</script>

<asp:Label id="result"runat="server"/>
</asp:Content>

With the AddDataToScope method, you can add an exception in the dashboard. Parameters, in order, are: an identifier of type uint, the category, the trace level of type int and the message.

Trace levels are as follows:

  • 1 = Critical
  • 4 = Exception
  • 6 = Assert
  • 8 = Warning
  • 10 = Unexpected
  • 15 = Monitorable

The execution result of this page to the dashboard is as follows:

SharePoint 2010 - Developer Dashboard 7

You may notice the addition to the section Asserts and Critical Events of our exception. The border of the dashboard is red because the page contains an exception.

By clicking on the link, you get more details about the error:

SharePoint 2010 - Developer Dashboard 8

 

The Developer Dashboard Visualizer project

The SharePoint 2010 Developer Dashboard Visualizer project is available on CodePlex. It can generate a chart using information from the dashboard.

The project is available as a feature. After downloaded, installed and activated via the menu Site Collection features, you get charts like this:

SharePoint 2010 - Developer Dashboard 9