when you want to build custom application in Visual Studio 2010 that are accessing the SharePoint 2010 object model, you will probably encounter the famous “File not found exception” when accessing an SPSite.This blog post shows you how to avoid this exception and how to setup your console application or ASP.NET web application the right way.
Overview
SharePoint 2010 runs on an x64 environment and supports the .NET Framework 3.5. All custom applications that want to access the SharePoint 2010 object model must follow the same environmental settings and target platform settings, i.e., run in a .NET Framework 3.5 environment and the platform x64 (or at least “Any CPU”). If you create a console application or an ASP.NET web application, Visual Studio 2010 will probably setup your environment for x84 and the .NET Framework 4.0. If you try to access the SharePoint 2010 object model with this configuration, you will encounter an “FileNotFoundException” when accessing the SPSite object.
In this post I’m going to show you how to configure…
- a console application accessing the SharePoint 2010 object model
- a web application accessing the SharePoint 2010 object model
Please note that this post assumes that you correctly add the necessary binary files (e.g., Microsoft.SharePoint) for yourself.
Accessing SharePoint 2010 object model with a console application
As already mentioned in the overview section, you have to ensure that your project environment is setup up to use the .NET Framework 3.5 and the platform target x64 (or Any CPU). You can change the settings of your projects by accessing the project properties.
Ensure that the target framework is .NET Framework 3.5 since the .NET Framework 4.0 is not yet supported.

Specify the target platform to “Any CPU”…

or x64…

The “FileNotFoundException” will be gone away if you start debugging your console application again.
Accessing SharePoint object model 2010 with an ASP.NET application
Accessing the SharePoint object model 2010 with an ASP.NET application follows the same strategy as in the console application. You have to configure the target framework to .NET Framework 3.5 and the target platform to x64 or “Any CPU”. You can change the properties in a similar way as we saw before with the console application.
However, you will not get the same development experience since the integrated web server of Visual Studio 2010 (Cassini) does not run in an x64 environment. Because of that, you will get again the “FileNotFoundException” when accessing the SharePoint 2010 object model. You will notice however, that the application runs like a charm when deployed in web server that supports x64 (such as IIS7). This is really a lame situation, since developing like that can become a pain in the ass.
Don’t worry! We don’t need necessarily to stick on the integrated web server of Visual Studio 2010. We can change our ASP.NET project configuration to be debuggable in an external web server such as IIS7. To do so, we need to follow these steps:
- configure your web application in Visual Studio 2010 to the target .NET Framework 3.5 and to run as x64 or “Any CPU” (similar as before with the console application)
- create a web application in IIS7 and configure the application pool to run for the .NET Framework Version v2.0 and x64. To ensure that the application runs in x64, just set “Enable 32-Bit Applications” to false.

- configure your web application in Visual Studio 2010 in the property page to “Use Custom Web Server” and specify there the url of your web application in IIS7

- publish the .NET web application to the target web application of IIS7
- debug your web application
After setting up the environment like that, after pressing the F5 button, Visual studio 2010 will automatically attach the debugger to the right IIS7 worker process. Now, you are ready to debug your ASP.NET web application without any problems.
Summary
Using the SharePoint 2010 object model in Visual Studio 2010 requires some configuration steps for your custom applications. This post showed you which settings are important to start with for console application projects and for ASP.NET web application projects.
Hope this helps,
Patrick Lamber