Thursday, March 4, 2010

SharePoint Object Model and step into code debugger does not work - problem solved

Wow, this was quite a headache... I recently had a situation where I developed a very simple code behind in SharePoint 2007. Nothing complex, really a simple piece of code.


If I used a simple piece of code (like below) I was able to build the code, deploy it to the bin folder and attach it to the w3wp process for debugging. I was able to step through the code and everything worked well.


But the problem I had was that as soon as I include any code that uses the Microsoft.SharePoint object model the debugger would NOT step into the code and when I launch the SharePoint Page which reference the code behind I will receive a Unexpected SharePoint Failure.

This is the code that worked:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Microsoft.SharePoint;

namespace ViewContract
{
 public class ViewContract: System.Web.UI.Page
 {
  protected override void OnInit(EventArgs e)
  {
   base.OnInit(e);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
   string strFindProblem;
   strFindProblem = "reached point 1";
  }
 }
}

Now, if I change the code to:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Microsoft.SharePoint;

namespace ViewContract
{
  public class ViewContract: System.Web.UI.Page
  {
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
      using (SPWeb contractweb = SPContext.Current.Web)     
      {
        a = contractweb.Url;
      }
    }
  }
}
 
I was not able to step through the code and my SharePoint Page failed.
 
So, the only way for me to fix this was to do the following:

  1. Open the web.config file located in C:\Inetpub\wwwroot\wss\VirtualDirectories\22990 where 22990 is the port number of your web application.
  2. Locate the following tag <trust level="WSS_Minimal" originUrl="" />
  3. Change  it to <trust level="WSS_Medium" originUrl="" />
  4. Save
  5. Attach code to w3wp process
  6. Open SharePoint Site
  7. Launch Code-behind page and wala...the breakpoint in the code is reached and you can now step through the code using Visual Studio.

0 comments:

Post a Comment