Debugging

On this page you read:


Debugging using Visual Studio

When you 're writing code for SharePoint, and your custom code runs into an error, by default SharePoint will log that error and it will present the user with a generic error through the browser. In order to view the actual error message through the browser, you will need to make 3 changes to your web.config:
  1. Change the SharePoint\SafeMode\@CallStack attribute to true.
  2. Change the System.Web\CustomErrors\@Mode attribute to off.
  3. Change the System.Web\Compilation\@Debug attribute to off.
Making these Changes will allow you to both attach to W3WP.exe and run your code in debug mode, and to view actual error message through the browser. Note that you should not make these changes in production environments. Finally if this is not an exact science! Whenever I run into an error in SharePoint programming, I usually look at the following four places in sequence:
  1. The browser window, assuming I have made the above-mentioned web.config changes.
  2. The ULS logs available at 14\Logs. You may need to tweak the logging level in central administration to view your actual error message. Also, you should try out this fantastic tool at
    http://code.msdn.microsoft.com/ulsviewer
  3. The event log, specially for WCF-related errors.
  4. The IIS log and tracing facilities.
If all these steps fail, I use the techniques described in this article:
http://www.code-magazine.com/Article.aspx?quickid=0907041




Add SharePoint to the VS project reference

If you wish to add Microsoft.SharePoint.dll to your project refrences (specially a console app or windows based app) on VS2010 but you can not find it on the refrences list on .Net tab of project refrences, or you get run-time error by adding the reference (Microsoft.SharePoint.dll) manully (by browsing the 14 hive directory), there may be a problem with your project settings. The resolution is:

  • Right-click on project name and select Properties
  • On the Application tab of the project properties find Target Framework.
  • If the Target Framework has been set to something like .Net Framework 4 Client Profile, you must change it to only .Net Framework 4 as is displyed below. 
     Problematic aspect of project settings is indicated below:

     Resolved aspect of project settings is indicated in the image below:
 
    By this resolution you should be able to easily add Microsoft,SharePoint.dll reference to your project.


Additional Lessons of the Day:
  1. When working with VS2010 to deliver new functionality into SharePoint, you should get good familiarity with the Object Model (= SharePoint API).
  2. A WebPart can be customised by administartor and can be personalised by user. A user can never customise a WebPart. (Terminology)