Posts Tagged ‘debugging’

Could not load web.config file. The given key was not present in the dictionary.

Friday, March 25th, 2011

Quick hint about debugging SharePoint projects in Visual Studio 2010. You may find a bizarre error where deploying projects/solutions is fine, but trying to debug doesn’t work. You see an error alert with the text:

“Could not load the Web.config configuration file. Check the file for any malformed XML elements, and try again. The following error occurred: The given key was not present in the dictionary.”

Checking the Web.config shows no malformed XML at all. One thing to check into is the configuration of alternate access mappings in Central Administration. For debugging to work, the Site URL property of your SharePoint project must match the URL of the Default zone for your web application.

Check: Central Administration > Application Management > Configure alternate access mappings > Edit public URLs

new SPSite – FileNotFoundException

Wednesday, February 2nd, 2011

It seems to me that every SharePoint developer, at some point in their career, will encounter this particular error. It’s one of those infuriatingly obtuse errors that could be caused by a multitude of different issues – either individually or cumulatively. In my case, I was in the middle of developing a SharePoint timer job and I had a particular piece of code that I wasn’t too sure about, and so wanted to test very quickly, over and over again – without the rigmarole of deploying/retracting solutions, hunting through Central Admin to run the job, etc. I just wanted to run this particular section of code against the SharePoint object model and see what happened. As is a common technique for this, I started up a fresh Console Application project, pasted in my code and figured that would be enough.

I was quite wrong and have spent the best part of the last 3 hours just trying to get the console app working. Since it was a standard C# console application, I added a reference to Microsoft.Sharepoint (14.0) and a using statement for the same assembly. The start of my code was the generic way just to get “in” to my SharePoint instance:

using (SPSite siteCollection = new SPSite(“http://sharepoint/”)) {

but when I then started debugging, it fell over on that line, with the “FileNotFoundException”. The web application and a site collection at that address definitely existed. Deploying a SharePoint solution in the usual way, worked. Running as the identity of the application pool, worked. So the key problem was in my own “Dev” account that I use to log in to the Dev machine.

So, it was off to Google for some advice. I won’t rehash everything I went through to get it working, but if you’re suffering from a similar issue, then you could start looking in any of these areas.

  • Is your devenv running as Administrator?
  • Does the process that your devenv/vshosts are running under have sufficient privilege to the Sharepoint farm? (Consider Site Collection admin, and possibly Farm Admin)
  • Does the user account have required privileges to the actual databases on the SQL Server?
  • Minimum of read/write on all content databases (common practice to give db_owner)
  • Read/write/db_owner main Config database and Administration Content database
  • WSS_CONTENT_APPLICATION_POOLS role on Config and Admin databases
  • Is your code compiled in 32-bit mode when you’re running on a 64-bit machine?

As it stands, I can’t be certain which of these “solutions” fixed the problem due to the fact that I tried them in various orders and did or didn’t restart various services/devenv/etc. throughout. But a combination of the above sorted me out – hopefully it can help you too. If there are any other common causes/solutions to this issue, feel free to leave them in the comments.

Some helpful links (check especially the comments in some of these):