Debug SharePoint macro
I've been working recently on a lot of code which needed debugging inside of SharePoint. One of the frustrating aspects of SharePoint development is the annoying debugging story. Click Debug, click Attach to Process, click your desired process(es), click Attach, click OK on each process popup... That's a lot of clicking to debug. So I put together a quick little macro for Visual Studio which allows me to attach to any running SharePoint process AND any of the development web server apps (those little things in the system tray) so that I can debug with a simple key combo (CTRL+SHFT+A, P). BTW - I totally stole this idea from like a dozen different authors which I forgot to write down so I can't give proper credit (sorry guys - not intentional), but I did modify the code so it fit my needs (both SharePoint and the dev web server at once). I frequently bounce IIS, hit the target web page, then run this:
Open Visual Studio, open the Macro Explorer, and put in the following code:
Sub Attach2ASPNetWP()
Dim attached As Boolean = False
Dim proc As EnvDTE.Process
Dim processToAttachTo As String
' name of the process to attach to
processToAttachTo = "WebDev.WebServer.EXE"
' iterate through all processes running on the local machine
For Each proc In DTE.Debugger.LocalProcesses
' if the last [X] characters of the process name = name of the process...
If (Right(proc.Name, Len(processToAttachTo)) = processToAttachTo) Then
' appach to the process
proc.Attach()
' set a flag that we've attached to the process & exit the for loop
attached = True
' Exit For
End If
Next
' now attach to the second process name
Dim secondProcessToAttachTo As String
Dim proc2 As EnvDTE.Process
secondProcessToAttachTo = "w3wp.exe"
For Each proc2 In DTE.Debugger.LocalProcesses
If (Right(proc2.Name, Len(secondProcessToAttachTo)) = secondProcessToAttachTo) Then
proc2.Attach()
attached = True
End If
Next
' if macro didn't find process running, notify user
If attached = False Then
MsgBox("Neither " & processToAttachTo & " nor " & secondProcessToAttachTo & " is running right now")
End If
End Sub
Note - this does need to be adjusted b/c it attaches to ALL SharePoint w3wp processes, not just your target process. For an easy way to identify the CORRECT SharePoint worker process to attach to, check out Doug Ware's helpful tip over on his blog at eLumenotion