Append your own messages to the Developer Dashboard in SP 2010 using Javascript

This is a great idea – if you want to quickly be able to identify where part of a solution is failing, you can use JavaScript to write to the Developer Dashboard – John Powell from Microsoft has some great info on his blog – http://blogs.msdn.com/b/johnwpowell/archive/2009/12/27/write-to-the-sharepoint-2010-developer-dashboard-with-javascript.aspx

The code he provides (duplicated below) allows you to “write” to the Dev Dashboard in the same way us Classic ASP developers used to write comments into the page to see what was going on (response.write, anyone?)

function DeveloperDashboardSection(sectionId, sectionTitle) { 
this.Title = sectionTitle; 
this.Id = sectionId; 
} 
DeveloperDashboardSection.prototype = { 
Id: '', 
Title: '', 
DeveloperDashboardVisible: function() { 
return$('DeveloperDashboard').length; 
}, 
EnsureSection: function() { 
var section = $('#'+ this.Id); 
if(!section.length) { 
var sectionHtml = "<table id='"+ this.Id + "' width='49%'><tbody><tr><th colspan='2'>"+ this.Title + "</th></tr></tbody></table>"; 
$('#CurrentScopeValues').append(sectionHtml); 
} 
}, 
AddEntry: function(context, message) { 
if(!this.DeveloperDashboardVisible) return; 
this.EnsureSection(); 
var entryHtml = "<tr><td nowrap='nowrap'><span>"+ context + "</span></td><td nowrap='nowrap'><span>"+ message + "</span></td></tr>"; 
$('#'+ this.Id).append(entryHtml); 
} 
}; 

To use the script, create an instance of the DeveloperDashboardSection and use the instance throughout your code to trace custom messages to the Dashboard:

var theSection = new DeveloperDashboardSection(‘sampleId’, ‘Custom Section’);
theSection.AddEntry(‘someMethod()’, ‘The method was called’);

The messages will appear on the Developer Dashboard in it’s own section:

image

When the Dashboard is not visible, the tracing calls are benign.

Nice!

Advertisements

About Brad Saide

I'm a SharePoint consultant. I'm also slowly going bald, seem to have a permanent spare tyre around my waist and enjoy socialising with friends over a beer or 10. The last 2 may possibly be related. Started working with SharePoint when the first version was in limited beta release (participated in the Technology Adoption Program while at Woolworths) and have been committed to the adoption of the technology as a business enabler ever since.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s