Showing posts with label debug. Show all posts
Showing posts with label debug. Show all posts

Tuesday, February 22, 2011

Powershell Script for Setting SharePoint's Developer Dashboard

Here's a script that can be used to enable, disable or allow toggle of the Developer Dashboard in SharePoint 2010. Note that if using the V3 interface, you can customize the master page to add the dashboard control so that it will render:
<SharePoint:DeveloperDashboard runat="server" />

$svc=[Microsoft.SharePoint.Administration.SPWebService]::ContentService

$ddsetting=$svc.DeveloperDashboardSettings

$choiceOnDemand = New-Object System.Management.Automation.Host.ChoiceDescription `
        "On &Demand","Dashboard can be toggled via icon near Welcome Menu"
$choiceOn = New-Object System.Management.Automation.Host.ChoiceDescription `
        "&On","Dashboard is on for all pages"
$choiceOff = New-Object System.Management.Automation.Host.ChoiceDescription `
        "O&ff","Dashboard is off"

$choices = [System.Management.Automation.Host.ChoiceDescription[]]($choiceOnDemand, $choiceOn, $choiceOff)
$caption = "Specify Developer Dashboard Setting for Farm"
$message = "Current the dashboard is: $($ddsetting.DisplayLevel)"
$result = $Host.UI.PromptForChoice($caption,$message,$choices,0)

switch ($result) {
    0 { Write-Host 'Dashboard is now On Demand, toggle via icon near Welcome Menu'
        $ddsetting.DisplayLevel=[Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand 
        break
      }
     
    1 { Write-Host 'Dashboard is now On'  
        $ddsetting.DisplayLevel=[Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On 
        break
      }
    2 { Write-Host "Dashboard is now Off"
        $ddsetting.DisplayLevel=[Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off 
        break
      }
      
    default { throw "Developer Error"}
}

if ($result -ne 2) {
  $ddsetting.TraceEnabled = $true
  Write-Host @"
  Trace was also enabled.
  Note: v3 interface requires this on the master page for the Dashboard to render:
     <SharePoint:DeveloperDashboard runat="server" />
"@
} else {
    $ddsetting.TraceEnabled = $false;
    Write-Host "Trace was also disabled."
}

$ddsetting.RequiredPermissions = 'EmptyMask'

$ddsetting.Update()

It also turns on trace, so it can be handy to find hard to debug problems.
In my case, it was helpful to find out why a delegate control wasn't rendering:
Tag(8e1n) Failed to create a user control from virtual path '/_controltemplates/CustomSearch.ascx': 
'Microsoft.SharePoint.WebControls.SearchArea, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' 
is not allowed here because it does not extend class 'System.Web.UI.UserControl'.


Based on approach from Praveen Battula's blog

Wednesday, April 21, 2010

Debugging Gotcha With .Net's System.Diagnostics.Debugger.Break()

In developing a SharePoint feature deployed at web application scope, I was trying to attach the debugger to the w3wp.exe process so I could see what was going on in my code. I used the iisapp command to try and pick the logical process... no dice. So then I connected to ALL of the w3wp process (there were 5 for them). It still wouldn't hit my breakpoint. So then I threw a

System.Diagnostics.Debugger.Break();

statement into my feature's code. When the feature activated, as expected I was given the option of selecting the debugger I wanted to use. So I chose my current VS2008 session. And here's the kicker--the next thing that happens is a dialog pops up telling you that "There is no source available at the current location". Now I had already dealt with all kinds of problems with missing symbols getting to this point, and I thought this was just the next level in the rathole I had been down on that issue. I was about to give up, but then I notided that the Call Stack pane was actually showing that the process was stopped down inside the System.Diagnostics assembly (which makes sense). All I had to do was a Debug > "Step Out" and I was back in my code, ready to debug.