So, over the years of working with SharePoint, I am sure like me you have had to debug code for SharePoint. If not, you are really missing out ☺
SharePoint even though using .NET is not the easiest sometimes to debug and track though little bugs that of course you never wrote in your code at all. Depending on what you are developing will determine what you use for debugging. In reality there are different tools for the types of code you write. The table below outlines some of the tools you could use:
Client Side using REST API
Server Side code using C#
As you can see from the table, client side code can really be debugged using any tool that can inspect the client page load or intercept the request. Server side code however needs to be done the full debugging way, using the development IDE to step through code.
When building solutions for SharePoint, knowing which process to attach to, in order to debug is just as important as the code you write. For example, building a web part that sits within SharePoint will mean that you need to connect to the “w3wp.exe” process or “IIS” as you probably know it better, whereas a SharePoint Timer Job is not available in “w3wp.exe” as it runs in the context of the “owstimer.exe” process for SharePoint.
If we were debugging a SharePoint Timer Job for example, we need to perform the following items.
- Generate the code and deploy
- Copy the debug symbols to the server we are going to debug into the “Microsoft .NET” folder:
3. Restart the SharePoint Timer Service
Once done we can then from within Visual Studio connect to the “owstimer.exe” service and begin debugging as we would use the “w3wp.exe” option.
Once selected we can debug our code the same way we did when testing it on a single server. This is a great way to debug code on other servers easily. This debugger will allow access to any of the services that reside on the other servers too.
SharePoint code debugging is not just isolated to Visual Studio and its tools. Debugging can be done using other tools such as WinDbg which allows you to break into the core code using a different tool. This does not allow you to step through the code but more see what has happened in the event of a crash dump being taken, or simply view any console statements that have been added to the code.
This tool is very useful and not just for debugging code, so be aware you will need to install most of the Windows Development SDK components to make it work. Once you have it all installed and ready you need to create a dump from the “w3wp.exe” process, by accessing the Task manager, right clicking the desired “w3wp.exe” process and choosing to create a dump file.
This can then be loaded into the WinDdg allowing for debugging and inspection. An article that was written quite a few years ago, by Kirk Evans from Microsoft, that still is a great example of this can be found using this link.
As you can see debugging SharePoint can be done using native Visual Studio and client side tools as well as Server Side Windows Debugging tools. The key is really determining what works best for you and your organization.