Debugging SharePoint 2010

Tue, Jan 14, 2014 2-minute read

I’m posting this here as, despite using this all the time, I still find myself looking it up.

There are a couple of things you can do, in your Dev environment, to assist with debugging SharePoint and your custom solutions.

CustomErrors and SafeMode

This should only be used in a Dev environment, but you can disable the “friendly” error messages in SharePoint and replace them with full exceptions, and, if desirable, a stack trace.

You can edit the web.config for your application. Note: SharePoint has various web.configs spewed about the place, and for this to work, you need to edit the one located in

C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TemplateLayoutsweb.config
[or C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TemplateAdminweb.config for Central Admin]

Find the block, and within the <system.web> section add

You can also enable a CallStack by editing the web.config of your application. This is the actual web.config which is wherever you configured it to be, but by default will be in

C:inetpubwwwrootwssVirtualDirectories

(easy to find by opening IIS Manager, and then right click > explore on your application.)

Find the node, and amend the attributes on to be CallStack=”true”.

Debugging memory leaks in the ULS

One key area you may find issues is in the disposal of SPSite and SPWeb objects. If you’re not doing this (either calling .Dispose() on your SPWeb and SPSite, or wrapping them in using statements) then stop all SharePoint development now and read this. (There’s a much longer list of SP best practices, here.)

Debugging object disposal is tricky, but you’ll usually see you have a problem when you see messages in your ULS logs along the line of “SPRequest object was reclaimed by the garbage collector. Please dispose of objects when you’re done with them.” or “Potentially excessive SPRequest objects on thread…”

By default you don’t get much in the way of extra info. But you can improve this by turning on a call stack in the ULS logs.

You can do this using the following Powershell script. Save this as a file with a .ps1 extension and then run it on demand - it’ll toggle the state for you.

if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}

[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

# Get Content Service of the farm
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

# Display and change the setting of property “CollectSPRequestAllocationCallStacks”
write-host “Current: ” $contentService.CollectSPRequestAllocationCallStacks
$contentService.CollectSPRequestAllocationCallStacks = -not $contentService.CollectSPRequestAllocationCallStacks
$contentService.Update()

write-host ” New: ” $contentService.CollectSPRequestAllocationCallStacks

Inspiration for this from here.

Let me know other tips for debugging SharePoint in the comments.