SharePoint 4 Developers

Additional reference guide in .NET / SharePoint Development

Macro SharePointDebugger

This macro will make your debug easier when dealing with W3wp and OWSTIMER processes, both utilized by SharePoint.

Hi folks,

I am starting a new section called “Tips and Tricks”, which main goal is to share tips based on real experiences.

In this post message a macro is going to be presented, that will make your debug easier when dealing with W3wp and OWSTIMER processes, both utilized by SharePoint. If you don’t know what they are and what they are used for, check them out:
•    OWSTimer – SharePoint Timer Service is a Windows service, which is installed together with Windows SharePoint Services (WSS). It is utilized to deal with scheduled jobs. If you are developing a Custom Timer Job, this is the process that you need to debug.
•    W3wp – This is an IIS work process related to the IIS application pool. If your web application has more than one application pool, so more than a single instance of w3wp.exe is running. This is a process that allocates much memory. If you are customizing SharePoint, this is the process that you need to debug.

In a nutshell, a macro is a collection of commands to automate repetitive actions in an IDE. E.g: Excel, Visual Studio.

What kind of repetitive actions are you talking about?


In order to debug any SharePoint customization, you need to attach processes from Visual Studio, available through: Debug > Attach to Process, as per Figure 1 and 2. And besides, it is needed to choose the wanted processes. Doing that once is not a problem, but it is laborious and time-consuming when you need to repeat it many times. And sometimes you receive some errors trying to attach processes.

1D806A05AA43783B_153_0[1]
Figure 1 – Attach to process

1D806A05AA43783B_153_1[1]
Figure 2 – Attach to process manually

What is the solution?


You can create your own macros in the Visual Studio to make your development easier. The code below is just an example of macro, specific to be used to debug those processes we talked before:

Code Snippet
  1. Imports System
  2. Imports EnvDTE
  3. Imports EnvDTE80
  4. Imports System.Diagnostics
  5.  
  6. Public Module SharePointDebugger
  7.  
  8.     Function AttachToProcess(ByVal processName As String) As Boolean
  9.         Dim proc As EnvDTE.Process
  10.         Dim attached As Boolean
  11.         For Each proc In DTE.Debugger.LocalProcesses
  12.             If (Right(proc.Name, Len(processName)) = processName) Then
  13.                 proc.Attach()
  14.                 attached = True
  15.             End If
  16.         Next
  17.  
  18.         Return attached
  19.     End Function
  20.  
  21.     Sub AttachDebuggerToWssTimerService()
  22.         Dim processToAttachTo As String = "OWSTIMER.EXE"
  23.  
  24.         If Not AttachToProcess(processToAttachTo) Then
  25.             MsgBox(processToAttachTo & " is not running")
  26.         End If
  27.     End Sub
  28.  
  29.     Sub AttachDebuggerToIIS()
  30.         Dim processToAttachTo As String = "w3wp.exe"
  31.  
  32.         If Not AttachToProcess(processToAttachTo) Then
  33.             MsgBox(processToAttachTo & " is not running")
  34.         End If
  35.     End Sub
  36.  
  37. End Module

Download

In order to test this code above you just need to add it into Visual Studio. Open the Macro Explorer (ALT+F8):

1D806A05AA43783B_153_2[1]
Figure 3 – Open Macro Explorer

Above all, if you downloaded the file SharePoint.vb, add it into Macros IDE:

1D806A05AA43783B_153_3[1]
Figure 4 – Open Macro IDE

1D806A05AA43783B_153_4[1]
Figure 5 – Add existing code

If you haven’t downloaded, add the code manually. In the root MyMacros, add a new module called SharepointDebugger:

1D806A05AA43783B_153_5[1]
Figure 6 – Add new module

Once the new module is added, it needs to be edited:

1D806A05AA43783B_153_6[1]
Figure 7 – Edit module

Paste the code into the module (just Subs and Function), and save:

1D806A05AA43783B_153_7[1]
Figure 8 – Paste the code

From now on, your macro is ready to be executed:

1D806A05AA43783B_153_8[1]
Figure 9 – View macros

Firstly, more than anything else, create a shortchut to call your macros. Wouldn’t you mind to run your macros using the mouse, right? Of course you mind! We are going to use shortcut keys. In order to configure it, access Tools > Options > Environment > Keyboard.

In the field “Show commands containing” type SharepointDebugger. You are about to see the list of macros being filtered automatically:

1D806A05AA43783B_153_9[1]
Figure 10 – Select macros

You can create your own shortcut Keys as you wish, clicking in the field “Press shortcut keys” and pressing the keys.
Utilize Ctrl + Shift + P, A. It means that you must press together Ctrl+Shift+P, loose them and press A (from Attach). In the end, click Assign:

1D806A05AA43783B_153_10[1]
Figure 11 – Add shortcut keys

Observe that you just created a shortcut key to AttachDebuggerToIIS, so you need to create another shortcut key for AttachDebuggerToWssTimerService.

In order to stop debugging, either you click over the button Stop in the Visual Studio toolbar, or you create another shortcut key to stop it. Search detachall:

1D806A05AA43783B_153_11[1]
Figure 12 – Add shortcut key for DetachAll

Utilise Ctrl + Shift + P, D (from detach).
These macros have helped me a lot when debugging SharePoint customizations and I hope it helps you.

References:
http://msdn.microsoft.com/en-us/library/b4c73967.aspx
http://office.microsoft.com/pt-br/help/HA100072101046.aspx
http://www.andrewconnell.com/blog/archive/2007/01/25/5855.aspx

Cheers,

Marcel Medina

Click here to read the same content in Portuguese.

blog comments powered by Disqus