Additional reference guide in .NET / SharePoint Development

I would like to inform that in this Codificando .Net e-Magazine issue my article about the News in SharePoint 2010 Development was published! (in Portuguese language)

This article is very important because many of the things that I have already talked about Sandboxed Solutions and other SharePoint 2010 news are contained therein, and all the development news were listed and commented.

I consider this e-magazine very important because it provides excellent technical materials for the .Net community free of charge!

Thanks to Codificando .Net and all the editorial team for this opportunity!

These last days I faced an error in SharePoint 2010 while I was creating BDC Model projects. At the moment of deploying my solution I got this error message in the Beta version:

Error occurred in deployment step 'Add Solution': Property 'SiteUrl' contains an invalid URL. Import failed with the following exception message: The site <site> could not be found in the Web application SPWebApplication Name=<webapplication>. Parameter name: properties

In the RTM version the error message changed a little bit, which is still not very clear:

Error occurred in deployment step 'Add Solution': The default web application could not be determined. Set the SiteUrl property in feature <feature> to the URL of the desired site and retry activation. Parameter name: properties

In order to solve this issue, I searched at the Internet for similar issues and I found out an excellent article of Jan Tielens that explains a workaround for that.

Just because I don’t simply like to add links to other sites with specific solutions, due to broken links, I am going to let this available here in my blog.

Getting the error message

1 – Create a new solution with a project of type Business Data Connectivity Model according Figure 1:

Figure 1 – BDCModel Project

2 – Try to deploy by Visual Studio, the error message above will show up (depending on the version).

Correcting the problem

1 – Open the Feature XML file (Figure 1) and notice there is just only one property:

When developing solutions, developers have the concern to deliver functional solutions that conform to the requested requirements. Although I always analyse the performance of my applications, most of the business solutions that I have worked performance is not a critical factor.

Depending on the scenario, what if performance is a requirement? How to get diagnostic information from your solutions in SharePoint 2010?

Precisely now there is the Developer Dashboard that brings a dashboard of tracing info.

In this article I will show how to enable the Developer Dashboard, how to use the APIs to display its Tracing info and how to display the rendering results of your page graphically.

Introducing the Developer Dashboard

Like something unknown, first of all I would like to introduce the Developer Dashboard, as we can see it below:

Figure 1 - SharePoint 2010 Dashboard Developer

Note: Note that the dashboard border is green, because neither asserts nor critical events are displayed. I'll talk about the color changes later in this post.

The left side shows the http handler events executed during the page rendering and their execution times, whilst in the right side a mix of comprehensive information and also details of the request can be found, such as the queries sent to database.

Interestingly, the links show the callstack of the message and other details, according to the figure below:

Figure 2 - Details of a database query

Once this is not unknown anymore for you, let's check what matters.

Enabling and disabling the Dashboard Developer

By default the Dashboard Developer is disabled and in order to display it there are three possibilities:

.Net code

STSADM

PowerShell

There are also three display levels of the Dashboard. As described below:

"On" - the dashboard will always be displayed;

"Off" - disables the dashboard;

"OnDemand" - adds an icon to the page, which allows the visualization of the dashboard only when clicked (toggling between On and Off).

The figure below displays this icon:

Figure 3 - Developer Dashboard Icon

NOTE: It is important to note that when the Dashboard is enabled using OnDemand, only Administrators and Site Collection Owners have access to it. Members and visitors will not see the icon. In this post the Developer Dashboard will be displayed using the display level "OnDemand", which in my point of view is the ideal.

.Net Code

A Feature that enables or disables the dashboard can be created, instead of using scripts for that. This way the activation or deactivation can be done directly through the Site Collection Features page.

Create a project in Visual Studio 2010 of Empty SharePoint Project called DeveloperDashboard. Choose the option of a Farm Solution. Your initial project should look like this:

Figure 4 – DeveloperDashboard Feature (Initial Project)

Add an Event Receiver to the Feature according the figure 5:

Figure 5 – Adding an Event Receiver

The class DeveloperDashboardEventReceiver will be created automatically, with all the methods commented out. Add the following code for the Feature Activation and Deactivation:

Measuring performance and adding messages via Tracing

Once the Developer Dashboard is active, we can proceed with the addition of Tracing messages in SharePoint 2010.

As already said this is an excellent way to measure the performance of code and add custom messages for tracking.

In order to measure the time of the code snippet that we intend to track, the class SPMonitoredScope should be used. This information will be available on the left side of the Dashboard.

In order to add log messages, warnings or assert messages, the class SPCriticalTraceCounter should be used. This information will be available on the right side of the dashboard, and a link will be available to visualize the message details.

NOTE: The callstack, which is in the message details, can add a few Kb on the page. Thus track the growth of the amount of custom messages that you have, they will increase the size (Kb) of your page if the Developer Dashboard is enabled.

Add a new project of Visual Web Part called Tracing. The solution should resemble the figure below:

This code contains two classes SPMonitoredScope and SPCriticalTraceCounter as already commented. It is important to know that the TraceLevel (3rd parameter of the method AddDataToScope) can be:

1 – Critical

4 - Exception (Watson)

6 – Assert

8 – Warning

10 – Unexpected

15 - Monitorable

Perform the addition of the Web Part (1), save it (2) and then visualize the Developer Dashboard (3) as shown below:

Figure 13 - Adding Web Part Tracing

Check out the Developer Dashboard, its border color is no longer green color as displayed in Figure 1. Differently it is in red color, which shows that asserts and critical events occurred (in this case the messages created):

Figure 14 - Time Scope and custom messages added

Click over one of the messages and note that the message details and callstack can be seen:

Developer Dashboard Visualizer

We can improve the Dashboard with a graphic display called Developer Dashboard Visualizer, which was created by Jaap Vossers and consists of a UserControl that makes use of JQuery to view the axis Events x Execution time (left side of the Developer Dashboard).

Download the wsp package and install it using the STSADM, as shown below:

Figure 16 - Enabling the Developer Dashboard Visualizer

The http handler events are displayed immediately above the dashboard:

Figure 17 - Graphical View

In this article we checked how to enable the SharePoint 2010 Developer Dashboard. We have checked that diagnostic information and tracing messages are output on the page to the browser that made the request. Such information may help to clarify errors or undesired results during the processing of your solution in SharePoint 2010.