Forums - C# Corner

Forum guidelines
sreenu kolusu

sreenu kolusu

  • 1.4k
  • 38
  • 130.8k

Debug tips for SharePoint

Aug 9 2012 2:00 PM
Goal of this post-
This post sums up all the different methods to get handy information when you need to debug a SharePoint piece of code.
Problematic overview
As a reminder, when an error occurs in a SharePoint page, the inexplicit error message appears:
Unknown error

Fortunately, you are able to display debug information, stack traces, detailed error messages, and more…

Solution, and many more
1. Set CustomError mode to Off and CallStack to true
When the message above occurs, the first step to execute is to configure the CustomError mode and the CallStack parameters in your web.config file:
  • Open the web.config file of your web application (e.g "C:\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config")
  • Search for CustomError node, and set its mode attribute to Off if it was set to On:<customErrors mode="Off" />
  • Search for CallStack attribute of SafeMode node, and set it to true if it was set to false: <SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10"
    TotalFileDependencies="50" AllowPageLevelTrace="false">
  • Save your changes and refresh your erroneous page:

 at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
 at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)

You are now able to determine which instruction had caused the error and what is the real error message.
2. Display trace
When developing SharePoint applications or pages, even if the page builds successfully, you may need more information on the server variables, the headers, the forms post values, the cookies, the page web controls, the request trace, etc…
This feature natively exists in ASP.NET; you just have to enable it into your web application configuration and activate it on a specific page, or into the web.config file of your Web application:
  • First, open your web.config file (e.g "C:\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config")
  • Search for AllowPageLevelTrace attribute of the SafeMode node (same line as CallStack attribute seen previously), and set it to true: <SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10"
    TotalFileDependencies="50" AllowPageLevelTrace="true">
Allow trace on a specific page:
  • Open your buggy ASPX page, and add a trace attribute to page directives and set to true, for example: <%@ Page Language="C#" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase"
    MasterPageFile="~/_layouts/application.master" Trace="true" %>
  • Save your changes, refresh your page, and scroll down, you'll see additional information:
Allow trace on the entire web site:

3. Using Visual Studio debugger
SharePoint developments can be debugged like console applications or windows applications by configuring the Visual Studio debugger.
Tip: you can attach your debugger to all the w3wp.exe processes, or determine which process is used by your current portal web application by opening a command prompt, and executing the following command:
%systemroot%\System32\cscript.exe %systemroot%\System32\iisapp.vbs

Once you have found the Application ID, you just have to attach your project to the right w3wp.exe process (the one that executes your current portal web application):
Visual Studio Debugger : Attach process
If you want to have more details about debugging tips within Visual Studio, you can find more documentation on MSDN:
Tip: when debugging a custom workflow, remember to select Workflow as code type when attaching the process. Otherwise, Visual Studio may crash without explicit error message.
4. Looking for LOG files
Depending on your diagnostic logging configuration (see Central Administration > Operations > Diagnostic logging), SharePoint can log events on text files located by default in: "C:\Program Files\Common files\Microsoft Shared\web server extensions\12\LOGS"
For instance, if you have to debug a custom Workflow developed with Visual Studio, open the last log file (by sorting them by date), place your cursor at the end of the file, and search for workflow infrastructure (direction: top). You'll see the detailed error that occurred while executing your workflow.
5. Write your own custom trace in SharePoint log files
Source code and usage sample can be found at the following MSDN url: